文章目录
- 1、logcat与log工具
- 2、通过log生成logcat日志
- 2.1、logcat日志等级
- 2.2、log指令说明
- 2.3、log生成日志指令
- 3、制作日志生成shell脚本
- 4、增加日志生成控制
- 5、附录
1、logcat与log工具
logcat:是Android操作系统中用于记录和查看系统日志的工具。它是Android开发和调试的重要工具之一,能够通过logcat定位诊断系统故障或应用程序的问题。
log:在android中默认集成的一个生成日志的工具,可以通过log指令来生成我们想要的各类logcat日志来方便测试。
2、通过log生成logcat日志
通过adb工具进入到android控制台:
# emulator-5554为设备名
adb -s emulator-5554 shell
2.1、logcat日志等级
执行logcat –help
查看指令说明,可以看到有日志等级,包括六种可以打印的类型(VERBOSE、DEBUG、INFO、WARNING、ERROR、FATAL)。另外一种S(Silent)表示静默,即上面所有类型的日志均不输出。
具体类型说明如下:从上往下等级逐渐升高
- VERBOSE:最低级别,用于详细的调试信息。
- DEBUG:用于调试信息,通常在开发过程中使用。
- INFO:用于普通信息,例如应用程序的运行状态。
- WARNING:用于警告信息,表明潜在的问题。
- ERROR:用于错误信息,表示应用程序或系统发生了错误。
- FATAL:用于表示严重的错误,通常伴随应用程序崩溃。
2.2、log指令说明
执行log --help
查看log说明内容:
-p
:默认打印INFO级别的日志,可以使用-p
参数来修改日志级别-t
:使用给定标签代替默认的‘log’内容
使用示例:
log -p v -t "syslog_test" "ivi----Log level of : VERBOSE"
执行完之后,执行logcat指令打印,即可得到我们指定的日志内容:
logcat | grep "syslog_test"
脚本说明:
定义日志等级为Verbose,指定日志TAG标签为“syslog_test”——系统日志测试,并输出具体的消息内容为“ivi----Log level of : VERBOSE”
log指令使用起来很简单也很方便。那么参照这个用法,可以生成不同等级不同TAG标签的内容。下面给出不同等级的适用实例。
2.3、log生成日志指令
我们先设置一个时间变量TIME,格式化为“年-月-日 时:分:秒”方便检查时间,后续加入到日志的打印中去:
TIME=`date +'%Y-%m-%d %H:%M:%S'`
- 生成VERBOSE,标签TAG为“LOG-TEST-1”:
log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."
- 生成DEBUG等级,标签TAG为“LOG-TEST-2”:
log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."
- 生成INFO等级,标签TAG为“LOG-TEST-3”:
log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."
- 生成WARNING等级,标签TAG为“LOG-TEST-4”:
log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."
- 生成ERROR等级,标签TAG为“LOG-TEST-5”:
log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."
- 生成FATAL等级,标签TAG为“LOG-TEST-6”:
log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."
Tips:上面的-p后接的参数,对应与log --help
中的说明:
3、制作日志生成shell脚本
在我们知道如何通过log生成单条不同等级和标签的用例后,那么我们接下来要通过这些指令,进行批量造日志了。在shell中使用循环语句来控制日志生成指令:
- 一组日志生成语句,包含下面这些:
TIME=`date +'%Y-%m-%d %H:%M:%S'`
log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."
log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."
log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."
log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."
log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."
log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."
- shell中的循环控制是这样:
while [condition]
do[sentence]
done
加入日志生成,其中再加入时延控制,usleep 200000
间隔200毫秒1组:
TIME=`date +'%Y-%m-%d %H:%M:%S'`
while true
doTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."usleep 200000
done
在Android6.0之后,可以通过集成的toybox工具集来使用vi工具,接下来使用vi来创建shell脚本:
# android控制面板执行
toybox vi /data/genlog.sh
键入i
后,输入上面的命令内容,粘贴内容之后输入Esc+:键,之后敲入wq
保存并退出编辑。
# 检查创建的脚本
cat /data/genlog.sh
赋予执行权限:
chmod +x genlog.sh
执行/data/genlog.sh
开始生成日志,logcat | grep "LOG-TEST"
可以看到能够正常批量生成日志:
也可以通过PC本地创建文本文件,再改为bash文件,使用adb push方式进行上传,使用这种方式需要注意下面两个编码问题:
- 1、我们通过Windows文本编辑器创建的文本内容,其格式默认为CR LF格式,在android设备中使用的unix编码,需要将编码转换为LF格式,否则在执行while、if等语句时会报错:
出现异常时,会报下面的syntax error: unmatched
错误:
- 2、制表符需要转换为空格,虽然不影响运行,但建议转换为空格执行:
4、增加日志生成控制
当我们想在运行时自由选择生成日志的级别,我们可以通过命令行参数传递进行日志级别控制:
脚本基本结构,通过while或if语句控制条件,之后通过传递的shell参数来判断执行哪一段语句。
1、参数传递:使用$n
表示第n个参数,其中$0
为文件名,$1
开始才是接的第一个参数
# 简单示例
# test.sh文件内容
echo "filename is : $0";
echo "the arg1 is : $1";
# 运行
./test.sh key1
参考:https://www.runoob.com/linux/linux-shell-passing-arguments.html
2、补全while条件:
定义日志等级对应数值1-6,另外加所有日志的生成场景,分别对应如下:
1 -- VERBOSE
2 -- DEBUG
3 -- INFO
4 -- WARNING
5 -- ERROR
6 -- FATAL
all -- 生成所有级别日志
修改while条件语句:
TIME=`date +'%Y-%m-%d %H:%M:%S'`
while [ "$1" == '1' ]
doTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."usleep 200000
donewhile [ "$1" == '2' ]
doTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."usleep 200000
donewhile [ "$1" == '3' ]
doTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."usleep 200000
donewhile [ "$1" == '4' ]
doTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."usleep 200000
donewhile [ "$1" == '5' ]
doTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."usleep 200000
donewhile [ "$1" == '6' ]
doTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."usleep 200000
donewhile [ "$1" == 'all' ]
doTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."usleep 200000
done
运行一下看看,能够成功的自由选择生成的级别了:
./genlog.sh 1
生成Verbose等级日志:
./genlog.sh all
生成所有级别日志:
./genlog.sh 3
生成Info级别日志:
至此已大工告成,上面的实现方法是将while语句与条件单独拆开进行判断,在shell中也可以使用if或case语句进行多条件判断,详见附录。
5、附录
- IF多条件结构:
if [ condition1 ]; then# 当 condition1 为真时执行
elif [ condition2 ]; then# 当 condition2 为真时执行
else# 当所有条件都不为真时执行
fi
将前面的脚本使用if结构进行修改:
TIME=`date +'%Y-%m-%d %H:%M:%S'`if [ "$1" == '1' ]; thenTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."usleep 200000elif [ "$1" == '2' ]; thenTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."usleep 200000elif [ "$1" == '3' ]; thenTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."usleep 200000elif [ "$1" == '4' ]; thenTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."usleep 200000elif [ "$1" == '5' ]; thenTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."usleep 200000elif [ "$1" == '6' ]; thenTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."usleep 200000elif [ "$1" == 'all' ]; thenTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."usleep 200000elseTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."usleep 200000
fi
修改完成,上面是只能打印1次,加入无条件while循环语句:
TIME=`date +'%Y-%m-%d %H:%M:%S'`if [ "$1" == '1' ]; thenwhile truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."usleep 200000doneelif [ "$1" == '2' ]; thenwhile truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."usleep 200000doneelif [ "$1" == '3' ]; thenwhile truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."usleep 200000doneelif [ "$1" == '4' ]; thenwhile truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."usleep 200000doneelif [ "$1" == '5' ]; thenwhile truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."usleep 200000doneelif [ "$1" == '6' ]; thenwhile truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."usleep 200000doneelif [ "$1" == 'all' ]; thenwhile truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."usleep 200000doneelsewhile truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."usleep 200000done
fi
控制正常:
- case多条件结构:
case variable inpattern1)# 当 variable 匹配 pattern1 时执行;;pattern2)# 当 variable 匹配 pattern2 时执行;;*)# 默认情况,当所有模式都不匹配时执行;;
esac
按照case结构进行修改:
TIME=`date +'%Y-%m-%d %H:%M:%S'`case "$1" in'1')while truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."usleep 200000done;;'2')while truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."usleep 200000done;;'3')while truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."usleep 200000done;;'4')while truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."usleep 200000done;;'5')while truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."usleep 200000done;;'6')while truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."usleep 200000done;;'all')while truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."usleep 200000done;;*)while truedoTIME=`date +'%Y-%m-%d %H:%M:%S'`log -p v -t "LOG-TEST-1" "GEN LOG level is : VERBOSE------${TIME}."log -p d -t "LOG-TEST-2" "GEN LOG level is : DEBUG--------${TIME}."log -p i -t "LOG-TEST-3" "GEN LOG level is : INFO---------${TIME}."log -p w -t "LOG-TEST-4" "GEN LOG level is : WARNING------${TIME}."log -p e -t "LOG-TEST-5" "GEN LOG level is : ERROR--------${TIME}."log -p f -t "LOG-TEST-6" "GEN LOG level is : FATAL--------${TIME}."usleep 200000done;;
esac