移动开发的过程中,在程序中打印日志是最有效的调试手段。Android系统提供了一套很好用日志API(android.util.Log),可以在代码中添加不同级别的日志。

在开发过程中logcat可以通过adb使用也可以在设备上直接使用,先看下帮助内容:

Usage: logcat [options] [filterspecs]
options include:
  -s              Set default filter to silent.
                  Like specifying filterspec '*:s'
  -f    Log to file. Default to stdout
  -r []   Rotate log every kbytes. (16 if unspecified). Requires -f
  -n       Sets max number of rotated logs to , default 4
  -v
     Sets the log print format, where

 is one of:

                  brief process tag thread raw time threadtime long

  -c              clear (flush) the entire log and exit
  -d              dump the log and then exit (don't block)
  -g              get the size of the log's ring buffer and exit
  -b      request alternate ring buffer
                  ('main' (default), 'radio', 'events')
  -B              output the log in binary
filterspecs are a series of
  [:priority]

where  is a log component tag (or * for all) and priority is:
  V    Verbose
  D    Debug
  I    Info
  W    Warn
  E    Error
  F    Fatal
  S    Silent (supress all output)

'*' means '*:d' and  by itself means :v

If not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.
If no filterspec is found, filter defaults to '*:I'

If not specified with -v, format is set from ANDROID_PRINTF_LOG
or defaults to "brief"

很简单,只要仔细看下就会使用。有几个小的使用心得分享下:

可以输出到文件(-f),个人认为在文件中看比较舒服点;

日志分为不同等级,当设置一个等级后,输出结果为高于该等级的所有日志;

配合“YouApp:X”在命令行最后加上“*:S”可以只输出YouApp相关的日志;

eg: logcat -f /data/test.log -v time :I *:S

另外,在NDK开发过程中也可以把日志写到logcat日志系统中,在NDK的<android/log.h>头文件中定义的:

/*
 * A variant of __android_log_print() that takes a va_list to list
 * additional parameters.
 */
int __android_log_vprint(int prio, const char *tag,
                         const char *fmt, va_list ap);

OK!尽情使用logcat吧!最后附一段自己封装的Util。

package com.theiter.androiddemologcat;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import android.util.Log;

public class Util {
	private static final String LOG_TAG = "THEITER-DEMOLOG";

	public static void debug( String msg )
	{
		Log.d( LOG_TAG, msg );
	}

	public static void debug( String msg, Throwable throwable )
	{
		Log.d( LOG_TAG, msg, throwable );
	}

	public static void info( String msg )
	{
		Log.i( LOG_TAG, msg );
	}

	public static void info( String msg, Throwable throwable )
	{
		Log.i( LOG_TAG, msg, throwable );
	}

	public static void warn( String msg )
	{
		Log.w( LOG_TAG, msg );
	}

	public static void warn( String msg, Throwable throwable )
	{
		Log.w( LOG_TAG, msg, throwable );
	}

	public static void error( String msg )
	{
		Log.e( LOG_TAG, msg );
	}

	public static void error( String msg, Throwable throwable )
	{
		Log.e( LOG_TAG, msg, throwable );
	}

	public static void printLog( int priority, String msg )
	{
		Log.println( priority, LOG_TAG, msg );
	}

	public static void writeLine(OutputStream os, PrintWriter logWriter, String value) throws IOException
	{
		String line = value + "\n";
		os.write( line.getBytes() );
		if( logWriter != null )
		{
			logWriter.println(value);
		}
	}

	public static void savelogtofile(String logtype,String logfmt,String filename,boolean alllog){

		try
		{
			String cmd = "logcat -f " + filename + " -v "+ logfmt+ " " + LOG_TAG + ":" + logtype;
			if(!alllog){
				cmd += " *:S";
			}

			Process process = Runtime.getRuntime().exec("su -c sh");
			OutputStream os = process.getOutputStream();
			Util.writeLine( os, null, cmd);

		}catch (Exception e) {
			throw new RuntimeException("Write log file error!");
		}

	}

}

转载请注明: 转载自iT人 – theiter

本文链接地址: logcat: Android日志系统

相关文章