# logger_util **Repository Path**: zhuyunjiandull/logger_util ## Basic Information - **Project Name**: logger_util - **Description**: flutter log utils - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-09-01 - **Last Updated**: 2025-02-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # logger_util 日志组件,引用了第三方库[logger](https://pub.dev/packages/logger)及[logger_flutter](https://pub.dev/packages/logger_flutter),增加的功能是创建一个全局 `Logger` 对象 `logger` ,方便在各个组件化的库使用。github地址[https://github.com/BrainCoTech/logger_util](https://github.com/BrainCoTech/logger_util)。 # FILE DIRECTORY //文件路径为:Android(应用的私有目录), iOS(Documents目录) 目录介绍如下: + log + 年月日文件夹 + 分文件存储的文件 + 年月日文件夹 + 分文件存储的文件 ... + filter(可能存在,有筛选条件的获取的时候才有这个文件夹,会被新的替换) + 年月日文件夹 + 分文件存储的文件 ... + log.zip(压缩上报的zip文件,会被新的替换) # INSTRUCTION FOR USE ``` //在项目启动处初始化基础信息 void main() async { WidgetsFlutterBinding.ensureInitialized(); await initLogger( //筛选条件,默认没有筛选 filter: null, //文件存储还是控制台打印,默认debug模式控制台打印,其他模式文件存储 isConsole: false, ); runApp(MyApp()); } ``` ``` //使用 //在需要的地方 LogUtil.logV('verbose'); LogUtil.logD('debug'); LogUtil.logI('info'); LogUtil.logW('warning'); LogUtil.logE('error'); LogUtil.logWTF('what the funck'); //分文件使用 LogUtil.logI('测试分文件存储info0', fileSubName: 'tvlian接'); LogUtil.logI('测试分文件存储info1', fileSubName: '电视连接'); LogUtil.logI('测试分文件存储info2', fileSubName: 'tv23455'); //带tag标记功能使用 LogUtil.logI('测试分文件存储info2', fileSubName: 'tv23455',tag: '随便标记'); //或者使用对象模式(方便统一设定fileSubName,不用每一条log都设置) var logObject = LogObject()..fileSubName = '统一设定的文件名'; //或者这样实例化 //var logObject = LogObject.instance..fileSubName = '统一设定的文件名'; //在使用的地方就不用设定fileSubName,即可! logObject.logV('verbose $indexSub'); logObject.logD('debug $indexSub'); logObject.logI('info $indexSub'); logObject.logW('warning $indexSub'); logObject.logE('error $indexSub'); logObject.log('测试分文件存储info2 $indexSub', level: Level.debug); logObject.logI('测试分文件存储info2', tag: '随便标记'); ``` >//logger存储的路径获取 ``` LogPath.getLoggerDir(); ``` >//主动获取压缩文件内容上报地址 ``` LogPath.getLoggerZip(); ``` # ATTENTION ### 使用LogUtil信息标注log建议遵循一定的规则来设定log的等级(严重等级从上到下依次递增) verbose[V]: 页面记录,页面上我们的标记触发,走了之类的一些信息 debug[D]: 一般的信息,指出细粒度信息事件对调试应用程序是非常有帮助的。 info[I]: 一般信息,消息在粗粒度级别上突出强调应用程序的运行过程。 warning[W]: 表明会出现潜在错误的情形。 error[E]: 指出虽然发生错误事件,但仍然不影响系统的继续运行。 what the funck[WTF]: 严重的错误,可能会导致程序不能正常运行. 类似System.exit(),整个项目都不能运行 ### 建议项目中统一使用LogUtil的方法记录log信息 ### 建议等级分明,避免滥用等级记录错误信息,(项目中可以直接从info向下开始使用,debug和verbose较弱可以直接用info取代) ## 一个项目中logger记录工具和等级统一即可(项目中log_base.dart中的logger是对外放开的,也可以项目中使用,使用方式参考logger的使用) ## Log level --- You can log with different levels: ```dart import package:logger_util/logger_util.dart; logger.v("Verbose log"); logger.d("Debug log"); logger.i("Info log"); logger.w("Warning log"); logger.e("Error log"); logger.wtf("What a terrible failure log"); ``` To show only specific log levels, you can set: ```dart Logger.level = Level.warning; ``` This hides all `verbose`, `debug` and `info` log events. ## Options --- pass some options: ```dart loggerOptions( filter: null, // Use the default LogFilter (-> only log in debug mode) printer: PrettyPrinter(), // Use the PrettyPrinter to format and print log output: null, // Use the default LogOutput (-> send everything to console) ); ``` If you use the `PrettyPrinter`, there are more options: ```dart var logger = Logger( printer: PrettyPrinter( methodCount: 2, // number of method calls to be displayed errorMethodCount: 8, // number of method calls if stacktrace is provided lineLength: 120, // width of the output colors: true, // Colorful log messages printEmojis: true, // Print an emoji for each log message printTime: false // Should each log print contain a timestamp ), ) ``` ## LogFilter --- The `LogFilter` decides which log events should be shown and which don't. The default implementation (`DevelopmentFilter`) shows all logs with `level >= Logger.level` while in debug mode. In release mode all logs are omitted. You can create your own `LogFilter` like this: ```dart class MyFilter extends LogFilter { @override bool shouldLog(LogEvent event) { return true; } } ``` This will show all logs even in release mode. (**NOT** a good idea) ## LogPrinter --- The `LogPrinter` creates and formats the output, which is then sent to the `LogOutput`. You can implement your own `LogPrinter`. This gives you maximum flexibility. A very basic printer could look like this: ```dart class MyPrinter extends LogPrinter { @override List log(LogEvent event) { return [event.message]; } } ``` If you created a cool `LogPrinter` which might be helpful to others, feel free to open a pull request. :) ### Colors Please note that all IDEs (VSCode, XCode, Android Studio, IntelliJ) do not support ANSI escape sequences in their terminal outputs. These escape sequences are used to color output. If using such an IDE do not configure colored output. ## LogOutput `LogOutput` sends the log lines to the desired destination. The default implementation (`ConsoleOutput`) send every line to the system console. ``` class ConsoleOutput extends LogOutput { @override void output(OutputEvent event) { for (var line in event.lines) { print(line); } } } ``` Possible future `LogOutput`s could send to a file, firebase or to Logcat. Feel free to open pull requests. ## logger_flutter extension The [logger_flutter](https://pub.dev/packages/logger_flutter) package is an extension for logger. You can add it to any Flutter app. Just shake the phone to show the console. Add the following code into your widget tree ```dart LogConsoleOnShake( child: Container() // Your widgets ), ``` //优化空间: 1.initlogger独立化对象,配置可变参数