|
| 1 | +# Logger |
| 2 | + |
| 3 | +[](https://pub.dartlang.org/packages/logger)   |
| 4 | + |
| 5 | +Small, easy to use and extensible logger which prints beautiful logs. |
| 6 | +Inspired by [logger](https://github.com/orhanobut/logger) for Android. |
| 7 | + |
| 8 | +**Show some ❤️ and star the repo to support the project** |
| 9 | + |
| 10 | +### Resources: |
| 11 | +- [Documentation](https://pub.dartlang.org/documentation/logger/latest/logger/) |
| 12 | +- [Pub Package](https://pub.dartlang.org/packages/logger) |
| 13 | +- [GitHub Repository](https://github.com/leisim/logger) |
| 14 | + |
| 15 | +## Getting Started |
| 16 | + |
| 17 | +Just create an instance of `Logger` and start logging: |
| 18 | +```dart |
| 19 | +var logger = Logger(); |
| 20 | +
|
| 21 | +logger.d("Logger is working!"); |
| 22 | +``` |
| 23 | + |
| 24 | +Instead of a string message, you can also pass other objects like `List`, `Map` or `Set`. |
| 25 | + |
| 26 | +## Output |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +## Log level |
| 31 | + |
| 32 | +You can log with different levels: |
| 33 | + |
| 34 | +```dart |
| 35 | +logger.v("Verbose log"); |
| 36 | +
|
| 37 | +logger.d("Debug log"); |
| 38 | +
|
| 39 | +logger.i("Info log"); |
| 40 | +
|
| 41 | +logger.w("Warning log"); |
| 42 | +
|
| 43 | +logger.e("Error log"); |
| 44 | +
|
| 45 | +logger.wtf("What a terrible failure log"); |
| 46 | +``` |
| 47 | + |
| 48 | +To show only specific log levels, you can set: |
| 49 | + |
| 50 | +```dart |
| 51 | +Logger.level = Level.warning; |
| 52 | +``` |
| 53 | + |
| 54 | +This hides all `verbose`, `debug` and `info` log events. |
| 55 | + |
| 56 | +## Options |
| 57 | + |
| 58 | +When creating a logger, you can pass some options: |
| 59 | + |
| 60 | +```dart |
| 61 | +var logger = Logger( |
| 62 | + printer: PrettyPrinter(), // Use the PrettyPrinter to format and print log |
| 63 | + filter: null, // Use the default LogFilter (-> only log in debug mode) |
| 64 | +); |
| 65 | +``` |
| 66 | + |
| 67 | +If you use the `PrettyPrinter`, there are more options: |
| 68 | + |
| 69 | +```dart |
| 70 | +var logger = Logger( |
| 71 | + printer: PrettyPrinter( |
| 72 | + methodCount: 2, // number of method calls to be displayed |
| 73 | + errorMethodCount: 8, // number of method calls if stacktrace is provided |
| 74 | + lineLength : 120 // width of the output |
| 75 | + ), |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +## LogPrinter |
| 80 | + |
| 81 | +You can implement your own `LogPrinter`. This gives you maximum flexibility. A very basic printer could look like this: |
| 82 | + |
| 83 | +``` |
| 84 | +class MyPrinter extends LogPrinter { |
| 85 | + @override |
| 86 | + void log(Level level, dynamic message, dynamic error, StackTrace stackTrace) { |
| 87 | + print(message); |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +If you created a cool `LogPrinter` which might be helpful to others, feel free to open a pull request. :) |
| 93 | + |
| 94 | +## LogFilter |
| 95 | + |
| 96 | +The `LogFilter` filters which logs should be shown and which don't. |
| 97 | +The default implementation shows all logs with `level >= Logger.level` while in debug mode. In release mode all logs are omitted. |
| 98 | + |
| 99 | +You can create your own `LogFilter` like this: |
| 100 | +```dart |
| 101 | +var filter = (Level level, dynamic message, dynamic error, StackTrace stackTrace) { |
| 102 | + return true; |
| 103 | +} |
| 104 | +``` |
| 105 | +This will show all logs even in release mode. (**NOT** a good idea) |
| 106 | + |
| 107 | +## MIT License |
| 108 | +``` |
| 109 | +Copyright (c) 2019 Simon Leier |
| 110 | +
|
| 111 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 112 | +of this software and associated documentation files (the "Software"), to deal |
| 113 | +in the Software without restriction, including without limitation the rights |
| 114 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 115 | +copies of the Software, and to permit persons to whom the Software is |
| 116 | +furnished to do so, subject to the following conditions: |
| 117 | +
|
| 118 | +The above copyright notice and this permission notice shall be included in all |
| 119 | +copies or substantial portions of the Software. |
| 120 | +
|
| 121 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 122 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 123 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 124 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 125 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 126 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 127 | +SOFTWARE. |
| 128 | +``` |
0 commit comments