前言

  日志信息对于程序员的作用我就不多说了,好的日志信息对于程序的调试,调优有着不可小觑的作用。随着项目的进展,你可能越发觉得安卓自带的日志类 android.util.Log 不能满足项目的需求,需要深度定制适合自己项目的 Logger 。

  如果你有以下的需求:

    1. 将 Android 日志信息记录到一个文件里面

    2. 深度定制化或者控制日志的显示,例如在发布产品后,考虑到性能,你需要阻止所有的日志信息

  那么该文章将尤其适合你。 本文章我将整合使用 android.util.Log 以及经典的 log4j 进行深度定制化日志管理。

项目实践

项目准备:

  0.  Android开发环境(Eclipse + ADT + AndroidSDK + ...)

  1. 下载 log4j JAR包

  2. 阅读相关文档,设置好开发环境(导入 log4j JAR包)

项目说明:

   该项目主要包括三个类:

     1. CLogger.java

     2. CLogConfiguration.java

     3. CLoggerHelper.java

深度定制化或者控制日志的显示

  这一小节中,我们将创建一个 Android-Logger 的 Android Project ,需要实现程序员可以控制 LogCat 中打印日志的类型。

    LoggerActivity.java            -> 用于测试

    CLogger.java                    -> 用于自定义 Logger ,封装 android.util.Log

    CLogConfiguration.java     -> 用于定义 Logger 的配置信息

  CLogger中我们定义的类型Level级别: VERBOSE < DEBUG < INFO < EARN < ERROR < What a Terrible Failure

  首先我们看一下测试类 LoggerActivity.java

 1 public class LoggerActivity extends Activity {
 2 
 3   private static final String TAG = "LoggerActivity";
 4 
 5   @Override
 6   public void onCreate(Bundle savedInstanceState) {
 7     super.onCreate(savedInstanceState);
 8     setContentView(R.layout.activity_logger);
 9 
10     CLogConfiguration logConfiguration = new CLogConfiguration();
11     logConfiguration.setLogcatLogShownLevel(Level.ERROR_INT);
12     CLogger.setLogConfiguration(logConfiguration);
13 
14     CLogger.v(TAG, "this is a VERBOSE log");
15     CLogger.d(TAG, "this is a DEBUG log");
16     CLogger.i(TAG, "this is a INFO log");
17     CLogger.w(TAG, "this is a WARN log");
18     CLogger.e(TAG, "this is a ERROR log");
19     CLogger.wtf(TAG, "this is a What a Terrible Failure log");
20   }
21 }

  备注:

    1. 第 14 ~ 19 行定义需要测试的 CLogger 类型

    2. 第 12 行将 CLogConfiguration 信息设置到 CLogger 中

    3. 第 10 ~ 11 行定义 CLogConfiguration 对象, 此处 logConfiguration.setLogcatLogShownLevel(Level.ERROR_INT), 那么只显示 "this is a ERROR log" 和 "this is a What a Terrible Failure log"。 如果将 "Level.ERROR_INT" 改为 "Level.DEBUG_INT",那么将会显示 "this is a DEBUG log", "this is a INFO log", "this is a WARN log", "this is a ERROR log", "this is a What a Terrible Failure log"。

  然后我们看一下 CLogger.java 类

  1 public class CLogger {
  2   private static CLogConfiguration logConfiguration = new CLogConfiguration();
  3 
  4   public static void setLogConfiguration(CLogConfiguration logConfiguration) {
  5     CLogger.logConfiguration = logConfiguration;
  6   }
  7 
  8   public static void configure() {
  9     logConfiguration.configure();
 10   }
 11 
 12   protected static String generateMessage(String tag, String msg) {
 13     tag = tag == null ? "" : tag;
 14     msg = msg == null ? "" : msg;
 15     return "(" + tag + ") " + msg;
 16   }
 17 
 18   /**
 19    * Send a VERBOSE log message and log the exception.
 20    * 
 21    * @param tag
 22    *          Used to identify the source of a log message.
 23    * @param msg
 24    *          The message you would like logged.
 25    */
 26   public static int v(String tag, String msg) {
 27     if (logConfiguration.getLogcatLogShownLevel() < Level.DEBUG_INT) {
 28       return Log.v(tag, msg);
 29     }
 30     return 0;
 31   }
 32 
 33   /**
 34    * Send a VERBOSE log message and log the exception.
 35    * 
 36    * @param tag
 37    *          Used to identify the source of a log message.
 38    * @param msg
 39    *          The message you would like logged.
 40    * @param tr
 41    *          An exception to log.
 42    */
 43   public static int v(String tag, String msg, Throwable tr) {
 44     if (logConfiguration.getLogcatLogShownLevel() < Level.DEBUG_INT) {
 45       return Log.v(tag, msg, tr);
 46     }
 47     return 0;
 48   }
 49 
 50   /**
 51    * Send a DEBUG log message and log the exception.
 52    * 
 53    * @param tag
 54    *          Used to identify the source of a log message.
 55    * @param msg
 56    *          The message you would like logged.
 57    */
 58   public static int d(String tag, String msg) {
 59     if (logConfiguration.getLogcatLogShownLevel() < Level.INFO_INT) {
 60       return Log.d(tag, msg);
 61     }
 62     return 0;
 63   }
 64 
 65   /**
 66    * Send a DEBUG log message and log the exception.
 67    * 
 68    * @param tag
 69    *          Used to identify the source of a log message.
 70    * @param msg
 71    *          The message you would like logged.
 72    * @param tr
 73    *          An exception to log.
 74    */
 75   public static int d(String tag, String msg, Throwable tr) {
 76     if (logConfiguration.getLogcatLogShownLevel() < Level.INFO_INT) {
 77       return Log.d(tag, msg, tr);
 78     }
 79     return 0;
 80   }
 81 
 82   /**
 83    * Send a INFO log message and log the exception.
 84    * 
 85    * @param tag
 86    *          Used to identify the source of a log message.
 87    * @param msg
 88    *          The message you would like logged.
 89    */
 90   public static int i(String tag, String msg) {
 91     if (logConfiguration.getLogcatLogShownLevel() < Level.WARN_INT) {
 92       return Log.i(tag, msg);
 93     }
 94     return 0;
 95   }
 96 
 97   /**
 98    * Send a INFO log message and log the exception.
 99    * 
100    * @param tag
101    *          Used to identify the source of a log message.
102    * @param msg
103    *          The message you would like logged.
104    * @param tr
105    *          An exception to log.
106    */
107   public static int i(String tag, String msg, Throwable tr) {
108     if (logConfiguration.getLogcatLogShownLevel() < Level.WARN_INT) {
109       return Log.i(tag, msg, tr);
110     }
111     return 0;
112   }
113 
114   /**
115    * Send a WARN log message and log the exception.
116    * 
117    * @param tag
118    *          Used to identify the source of a log message.
119    * @param msg
120    *          The message you would like logged.
121    */
122   public static int w(String tag, String msg) {
123     if (logConfiguration.getLogcatLogShownLevel() < Level.ERROR_INT) {
124       return Log.w(tag, msg);
125     }
126     return 0;
127   }
128 
129   /**
130    * Send a WARN log message and log the exception.
131    * 
132    * @param tag
133    *          Used to identify the source of a log message.
134    * @param msg
135    *          The message you would like logged.
136    * @param tr
137    *          An exception to log.
138    */
139   public static int w(String tag, String msg, Throwable tr) {
140     if (logConfiguration.getLogcatLogShownLevel() < Level.ERROR_INT) {
141       return Log.w(tag, msg, tr);
142     }
143     return 0;
144   }
145 
146   /**
147    * Send a ERROR log message and log the exception.
148    * 
149    * @param tag
150    *          Used to identify the source of a log message.
151    * @param msg
152    *          The message you would like logged.
153    */
154   public static int e(String tag, String msg) {
155     return Log.e(tag, msg);
156   }
157 
158   /**
159    * Send a ERROR log message and log the exception.
160    * 
161    * @param tag
162    *          Used to identify the source of a log message.
163    * @param msg
164    *          The message you would like logged.
165    * @param tr
166    *          An exception to log.
167    */
168   public static int e(String tag, String msg, Throwable tr) {
169     return Log.e(tag, msg, tr);
170   }
171 
172   /**
173    * What a Terrible Failure: Report a condition that should never happen.
174    * 
175    * @param tag
176    *          Used to identify the source of a log message.
177    * @param tr
178    *          An exception to log.
179    */
180   public static int wtf(String tag, Throwable tr) {
181     return Log.wtf(tag, tr);
182   }
183 
184   /**
185    * What a Terrible Failure: Report a condition that should never happen.
186    * 
187    * @param tag
188    *          Used to identify the source of a log message.
189    * @param msg
190    *          he message you would like logged.
191    */
192   public static int wtf(String tag, String msg) {
193     return Log.wtf(tag, msg);
194   }
195 
196   /**
197    * What a Terrible Failure: Report a condition that should never happen.
198    * 
199    * @param tag
200    *          Used to identify the source of a log message.
201    * @param msg
202    *          he message you would like logged.
203    * @param tr
204    *          An exception to log. May be null.
205    */
206   public static int wtf(String tag, String msg, Throwable tr) {
207     return Log.wtf(tag, msg, tr);
208   }
209 
210 }

相关文章:

  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2021-09-09
  • 2021-12-29
  • 2021-10-16
  • 2022-12-23
  • 2022-02-16
猜你喜欢
  • 2022-12-23
  • 2021-06-14
  • 2021-08-12
  • 2021-10-07
  • 2022-12-23
相关资源
相似解决方案