使用 ADB LogCat 查看在Android真机上 Unity debug.log 输出日志

简介: 在使用unity开的过程中查看输出日志是避免不了的,但是在真机上遇到卡死或者尤其是闪退的时候怎么办呢?这里给大家介绍一个简单便捷的android真机查看日志方法参考了以下3位作者的文章:http://blog.

在使用unity开的过程中查看输出日志是避免不了的,但是在真机上遇到卡死或者尤其是闪退的时候怎么办呢?这里给大家介绍一个简单便捷的android真机查看日志方法

参考了以下3位作者的文章:

在使用工具之前需要配置相应的环境变量,没有配置的同学请看

Unity SDK JDK 环境配置 避免各种莫名其妙的报错

配合之前Unity Debug输出到屏幕并保存到本地服用效果更佳,早日和顽固Bug说ByeBye

首先在WIN键+R快捷键调出运行控制台,输入cmd
img_e9c58856284ab41a7025d3df529bec54.png
在命令行中输入 adb logcat --help 命令, 就可以显示该命令的帮助信息;
img_40789342b10a9e466467b09d6cd86b0a.gif
Usage: logcat [options] [filterspecs]
options include:
  -s              Set default filter to silent. Equivalent to filterspec '*:S'
  -f <file>, --file=<file>               Log to file. Default is stdout
  -r <kbytes>, --rotate-kbytes=<kbytes>
                  Rotate log every kbytes. Requires -f option
  -n <count>, --rotate-count=<count>
                  Sets max number of rotated logs to <count>, default 4
  --id=<id>       If the signature id for logging to file changes, then clear
                  the fileset and continue
  -v <format>, --format=<format>
                  Sets log print format verb and adverbs, where <format> is:
                    brief help long process raw tag thread threadtime time
                  and individually flagged modifying adverbs can be added:
                    color descriptive epoch monotonic printable uid
                    usec UTC year zone
                  Multiple -v parameters or comma separated list of format and
                  format modifiers are allowed.
  -D, --dividers  Print dividers between each log buffer
  -c, --clear     Clear (flush) the entire log and exit
                  if Log to File specified, clear fileset instead
  -d              Dump the log and then exit (don't block)
  -e <expr>, --regex=<expr>
                  Only print lines where the log message matches <expr>
                  where <expr> is a regular expression
  -m <count>, --max-count=<count>
                  Quit after printing <count> lines. This is meant to be
                  paired with --regex, but will work on its own.
  --print         Paired with --regex and --max-count to let content bypass
                  regex filter but still stop at number of matches.
  -t <count>      Print only the most recent <count> lines (implies -d)
  -t '<time>'     Print most recent lines since specified time (implies -d)
  -T <count>      Print only the most recent <count> lines (does not imply -d)
  -T '<time>'     Print most recent lines since specified time (not imply -d)
                  count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'
                  'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format
  -g, --buffer-size                      Get the size of the ring buffer.
  -G <size>, --buffer-size=<size>
                  Set size of log ring buffer, may suffix with K or M.
  -L, --last      Dump logs from prior to last reboot
  -b <buffer>, --buffer=<buffer>         Request alternate ring buffer, 'main',
                  'system', 'radio', 'events', 'crash', 'default' or 'all'.
                  Multiple -b parameters or comma separated list of buffers are
                  allowed. Buffers interleaved. Default -b main,system,crash.
  -B, --binary    Output the log in binary.
  -S, --statistics                       Output statistics.
  -p, --prune     Print prune white and ~black list. Service is specified as
                  UID, UID/PID or /PID. Weighed for quicker pruning if prefix
                  with ~, otherwise weighed for longevity if unadorned. All
                  other pruning activity is oldest first. Special case ~!
                  represents an automatic quicker pruning for the noisiest
                  UID as determined by the current statistics.
  -P '<list> ...', --prune='<list> ...'
                  Set prune white and ~black list, using same format as
                  listed above. Must be quoted.
  --pid=<pid>     Only prints logs from the given pid.
  --wrap          Sleep for 2 hours or when buffer about to wrap whichever
                  comes first. Improves efficiency of polling by providing
                  an about-to-wrap wakeup.

filterspecs are a series of
  <tag>[:priority]

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

'*' by itself means '*:D' and <tag> by itself means <tag>:V.
If no '*' filterspec or -s on command line, all filter defaults to '*:V'.
eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.

If not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.

If not specified with -v on command line, format is set from ANDROID_PRINTF_LOG
or defaults to "threadtime"

控制台窗口输入指令格式为:[adb] logcat [<option>] ... [<filter-spec>] ...

其中的 [<option>] 的指令都有:

  • "-s"选项 : 设置输出日志的标签, 只显示该标签的日志;
  • "-f"选项 : 将日志输出到文件, 默认输出到标准输出流中, -f 参数执行不成功;
  • "-r"选项 : 按照每千字节输出日志, 需要 -f 参数, 不过这个命令没有执行成功;
  • "-n"选项 : 设置日志输出的最大数目, 需要 -r 参数, 这个执行 感觉 跟 adb logcat 效果一样;
  • "-v"选项 : 设置日志的输出格式, 注意只能设置一项;
  • "-c"选项 : 清空所有的日志缓存信息;
  • "-d"选项 : 将缓存的日志输出到屏幕上, 并且不会阻塞;
  • "-t"选项 : 输出最近的几行日志, 输出完退出, 不阻塞;
  • "-g"选项 : 查看日志缓冲区信息;
  • "-b"选项 : 加载一个日志缓冲区, 默认是 main, 下面详解;
  • "-B"选项 : 以二进制形式输出日志;

在[<option>]后面接着的是[<filter-spec>],意思输出相应的标签内容,例如我们要查看unity的Log信息,我们就输 adb logcat -s Unity只查看unity的信息 中文显示为乱码 如果只输入adb logcat 会打印android中的所有信息,对一些不是unity内部出现的报错定位也是很有帮助的

img_88262199718f503b107ca2715e0153dd.gif

这样输出后就算闪退我们也不怕了,但是新的问题来了,这个窗口不方便查看,而且中文还是乱码,那怎么办呢? 接着我们按照[adb] logcat [<option>] ... [<filter-spec>] ...这个格式输入 adb logcat -s Unity -d > C:\UnityDrictory\UnityADBLog.txt

输出日志信息到文件 : "-f"选项 : 该选向后面跟着输入日志的文件, 使用adb logcat -f /sdcard/log.txt 命令, 注意这个log文件是输出到手机上,需要指定合适的路径。PC上用上面的

img_6f0acb52dbcc21ef7d24ccceeceae7ac.gif

这样我们就可以以文本的方式查看Log日志了,而且中文不再是乱码

根据[<option>]选项中的 -f 的说明:默认输出到标准输出流中,所以在查看下次运行的日志时候,需要提前清空日志缓存 adb logcat -c ,这样就不会把上次不用的日志打印出来了

img_1f29d9cce2d605e5014ba56c33d75701.gif

而且配合Notepad++查看txt就更方便了,它会自动格几秒提醒你是否要更新文本,更新了就获取到了最新的打印日志

Notepad++下载地址

img_7ee6d4444df82fc96f85b1c6c366fa08.png
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
8天前
|
Shell Android开发
Android系统 adb shell push/pull 禁止特定文件
Android系统 adb shell push/pull 禁止特定文件
35 1
|
6天前
|
关系型数据库 MySQL 数据库
mysql数据库bin-log日志管理
mysql数据库bin-log日志管理
|
6天前
|
存储 关系型数据库 数据库
关系型数据库文件方式存储LOG FILE(日志文件)
【5月更文挑战第11天】关系型数据库文件方式存储LOG FILE(日志文件)
30 1
|
7天前
|
Shell Android开发
ADB更改Android设备屏幕显示方向
ADB更改Android设备屏幕显示方向
17 5
|
7天前
|
运维 监控 安全
Java一分钟之-Log4j与日志记录的重要性
【5月更文挑战第16天】Log4j是Java常用的日志框架,用于灵活地记录程序状态和调试问题。通过设置日志级别和过滤器,可避免日志输出混乱。为防止日志文件过大,可配置滚动策略。关注日志安全性,如Log4j 2.x的CVE-2021-44228漏洞,及时更新至安全版本。合理使用日志能提升故障排查和系统监控效率。
49 0
|
8天前
|
Shell Android开发
Android Activity重写dump方法实现通过adb调试代码
Android Activity重写dump方法实现通过adb调试代码
17 0
|
8天前
|
机器学习/深度学习 Shell Perl
logcat 只打印符合包名的log
logcat 只打印符合包名的log
14 1
|
8天前
|
C++
JNI Log 日志输出
JNI Log 日志输出
49 1
|
8天前
|
存储 运维 大数据
聊聊日志硬扫描,阿里 Log Scan 的设计与实践
泛日志(Log/Trace/Metric)是大数据的重要组成,伴随着每一年业务峰值的新脉冲,日志数据量在快速增长。同时,业务数字化运营、软件可观测性等浪潮又在对日志的存储、计算提出更高的要求。
219 6
|
8天前
|
Java Android开发
Android 对adb命令的拦截
Android 对adb命令的拦截
18 2