安卓 log日志框架
Posted zhen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓 log日志框架相关的知识,希望对你有一定的参考价值。
◆elvishew/xLog
框架特性介绍
Global config(tag, formatters...) or log-based config
Support printing any object and customizable object formatter
Support printing array
Support printing long log (No 4K limitation)
XML and JSON formatted
Thread information (Thread name etc. Can be customized)
Stack trace information (Configurable call stack depth, with file name, method name, line number)
Support log interceptors
Save logs in file (Configurable file naming and backup strategy)
Good looking in android Studio
Easy to use, powerful in customization
xLog支持数组、object、XML、JSON等数据格式打印,支持Log保存本地,支持log级别控制。及格式化打印配置。
代码部分
集成到项目:
S1.添加依赖 compile \'com.elvishew:xlog:1.4.0\'或导入library源码库;
S2.初始化log;
S3.调用log;
API:
初始化 LogConfiguration config = new LogConfiguration.Builder() .logLevel(BuildConfig.DEBUG ? LogLevel.ALL // Specify log level, logs below this level won\'t be printed, default: LogLevel.ALL : LogLevel.NONE) .tag("MY_TAG") // Specify TAG, default: "X-LOG" .t() // Enable thread info, disabled by default .st(2) // Enable stack trace info with depth 2, disabled by default .b() // Enable border, disabled by default .jsonFormatter(new MyJsonFormatter()) // Default: DefaultJsonFormatter .xmlFormatter(new MyXmlFormatter()) // Default: DefaultXmlFormatter .throwableFormatter(new MyThrowableFormatter()) // Default: DefaultThrowableFormatter .threadFormatter(new MyThreadFormatter()) // Default: DefaultThreadFormatter .stackTraceFormatter(new MyStackTraceFormatter()) // Default: DefaultStackTraceFormatter .borderFormatter(new MyBoardFormatter()) // Default: DefaultBorderFormatter .addObjectFormatter(AnyClass.class, // Add formatter for specific class of object new AnyClassObjectFormatter()) // Use Object.toString() by default .addInterceptor(new BlacklistTagsFilterInterceptor( // Add blacklist tags filter "blacklist1", "blacklist2", "blacklist3")) .addInterceptor(new MyInterceptor()) // Add a log interceptor .build(); Printer androidPrinter = new AndroidPrinter(); // Printer that print the log using android.util.Log Printer consolePrinter = new ConsolePrinter(); // Printer that print the log to console using System.out Printer filePrinter = new FilePrinter // Printer that print the log to the file system .Builder("/sdcard/xlog/") // Specify the path to save log file .fileNameGenerator(new DateFileNameGenerator()) // Default: ChangelessFileNameGenerator("log") .backupStrategy(new NeverBackupStrategy()) // Default: FileSizeBackupStrategy(1024 * 1024) .logFlattener(new MyFlattener()) // Default: DefaultFlattener .build(); XLog.init( // Initialize XLog config, // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build() androidPrinter, // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used. consolePrinter, filePrinter); log打印: XLog.d("Simple message") XLog.d("My name is %s", "Elvis"); XLog.d("An exception caught", exception); XLog.d(object); XLog.d(array); XLog.json(unformattedJsonString); XLog.xml(unformattedXmlString);
参考:
https://github.com/elvishew/xLog
◆orhanobut/logger
框架特性介绍
Simple, pretty and powerful logger for android
支持基本信息打印、对象打印、XML/JSON打印、格式化打印,最新版本已移除LOG级别控制。
代码部分
集成到项目:
S1.添加依赖 compile \'com.orhanobut:logger:2.1.1\'或导入library源码库;
S2.初始化logger;
S3.调用logger;
API:
初始化 FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder() .showThreadInfo(false) // (Optional) Whether to show thread info or not. Default true .methodCount(0) // (Optional) How many method line to show. Default 2 .methodOffset(7) // (Optional) Hides internal method calls up to offset. Default 5 .logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat .tag("My custom tag") // (Optional) Global tag for every log. Default PRETTY_LOGGER .build(); Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy)); Logger.addLogAdapter(new AndroidLogAdapter() { @Override public boolean isLoggable(int priority, String tag) { return BuildConfig.DEBUG; } }); FormatStrategy formatStrategy = CsvFormatStrategy.newBuilder() .tag("custom") .build(); Logger.addLogAdapter(new DiskLogAdapter(formatStrategy)); log打印: Logger.d("debug"); Logger.e("error"); Logger.w("warning"); Logger.v("verbose"); Logger.i("information"); Logger.wtf("wtf!!!!"); Logger.d("hello %s", "world"); Logger.d(MAP); Logger.d(SET); Logger.d(LIST); Logger.d(ARRAY); Logger.json(JSON_CONTENT); Logger.xml(XML_CONTENT);
参考:
https://github.com/orhanobut/logger
pengwei1024/LogUtils
框架特性介绍
支持直接打印数据集合,如List、Set、Map、数组等
全局配置log输出
个性化设置Tag
准确显示调用方法、行,快速定位所在文件位置
支持android系统对象Intent、Bundle打印
提供release-no-op版本
支持日志写入文件
代码部分
集成到项目:
S1.导入library源码库;
compile \'com.apkfuns.logutils:library:1.5.1.1\'
compile files(\'libs/okio-1.13.0.jar\')
S2.初始化logutils;
S3.调用log方法;
API:
初始化 LogUtils.getLogConfig() .configAllowLog(true) .configTagPrefix("MyAppName") .configShowBorders(true) .configFormatTag("%d{HH:mm:ss:SSS} %t %c{-5}") # 支持写入日志到文件 LogUtils.getLog2FileConfig().configLog2FileEnable(true) // targetSdkVersion >= 23 需要确保有写sdcard权限 .configLog2FilePath("/sdcard/项目文件夹/logs/") .configLog2FileNameFormat("%d{yyyyMMdd}.txt") .configLogFileEngine(new LogFileEngineFactory()); log打印: // 输出字符串 LogUtils.d("12345"); // 输出参数 LogUtils.d("12%s3%d45", "a", 0); // 输出异常 LogUtils.d(new NullPointerException("12345")); // 输出对象 Person person = new Person(); person.setAge(11); person.setName("pengwei"); person.setScore(37.5f); LogUtils.d(person); // 对象为空 LogUtils.d(null); // 输出json(json默认debug打印) String json = "{\'a\':\'b\',\'c\':{\'aa\':234,\'dd\':{\'az\':12}}}"; LogUtils.json(json); // 打印数据集合 List<Person> list1 = new ArrayList<>(); for(int i = 0; i < 4; i++){ list1.add(person); } LogUtils.d(list1); // 打印数组 double[][] doubles = {{1.2, 1.6, 1.7, 30, 33}, {1.2, 1.6, 1.7, 30, 33}, {1.2, 1.6, 1.7, 30, 33}, {1.2, 1.6, 1.7, 30, 33}}; LogUtils.d(doubles); // 自定义tag LogUtils.tag("我是自定义tag").d("我是打印内容"); // 其他用法 LogUtils.v("12345"); LogUtils.i("12345"); LogUtils.w("12345"); LogUtils.e("12345"); LogUtils.wtf("12345");
参考:
以上是关于安卓 log日志框架的主要内容,如果未能解决你的问题,请参考以下文章