|
| 1 | +# NDK Custom Logger |
| 2 | + |
| 3 | +The NDK now uses a custom, modular logger implementation that is WASM-compatible (no `dart:io` dependency). |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **WASM Compatible**: No dependency on `dart:io`, making it suitable for web/WASM targets |
| 8 | +- **Modular Output System**: Support for multiple log outputs (console, file, network, etc.) |
| 9 | +- **Log Levels**: Support for trace, debug, info, warning, error, fatal, all, and off levels |
| 10 | +- **Flexible Configuration**: Easy to configure and customize |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +### Basic Usage |
| 15 | + |
| 16 | +The logger is already configured and ready to use: |
| 17 | + |
| 18 | +```dart |
| 19 | +import 'package:ndk/ndk.dart'; |
| 20 | +
|
| 21 | +// Use the static logger instance |
| 22 | +Logger.log.d('Debug message'); |
| 23 | +Logger.log.i('Info message'); |
| 24 | +Logger.log.w('Warning message'); |
| 25 | +Logger.log.e('Error message'); |
| 26 | +``` |
| 27 | + |
| 28 | +### Setting Log Level |
| 29 | + |
| 30 | +You can change the log level at runtime: |
| 31 | + |
| 32 | +```dart |
| 33 | +// Set to debug level |
| 34 | +Logger.setLogLevel(LogLevel.debug); |
| 35 | +
|
| 36 | +// Or use the logLevels constants |
| 37 | +Logger.setLogLevel(Logger.logLevels.warning); |
| 38 | +``` |
| 39 | + |
| 40 | +### Available Log Levels |
| 41 | + |
| 42 | +- `LogLevel.all` - Log everything (lowest priority) |
| 43 | +- `LogLevel.trace` - Very detailed debugging |
| 44 | +- `LogLevel.debug` - Debugging information |
| 45 | +- `LogLevel.info` - Informational messages |
| 46 | +- `LogLevel.warning` - Warning messages (default) |
| 47 | +- `LogLevel.error` - Error messages |
| 48 | +- `LogLevel.fatal` - Fatal error messages |
| 49 | +- `LogLevel.off` - Disable logging |
| 50 | + |
| 51 | +### Custom Log Outputs |
| 52 | + |
| 53 | +You can create custom log outputs by implementing the `LogOutput` interface: |
| 54 | + |
| 55 | +```dart |
| 56 | +import 'package:ndk/ndk.dart'; |
| 57 | +
|
| 58 | +class MyCustomOutput implements LogOutput { |
| 59 | + @override |
| 60 | + void output(LogEvent event) { |
| 61 | + // Handle the log event |
| 62 | + print('Custom: [${event.level.name}] ${event.message}'); |
| 63 | + } |
| 64 | +
|
| 65 | + @override |
| 66 | + void destroy() { |
| 67 | + // Cleanup when the logger is destroyed |
| 68 | + } |
| 69 | +} |
| 70 | +
|
| 71 | +// Add the custom output to the logger |
| 72 | +final customOutput = MyCustomOutput(); |
| 73 | +Logger.log.addOutput(customOutput); |
| 74 | +
|
| 75 | +// Now logs will go to both console and your custom output |
| 76 | +Logger.log.i('This goes to multiple outputs'); |
| 77 | +
|
| 78 | +// Remove when done |
| 79 | +Logger.log.removeOutput(customOutput); |
| 80 | +``` |
| 81 | + |
| 82 | +### Multiple Outputs |
| 83 | + |
| 84 | +The logger supports sending logs to multiple outputs simultaneously: |
| 85 | + |
| 86 | +```dart |
| 87 | +// Add multiple outputs |
| 88 | +Logger.log.addOutput(fileOutput); |
| 89 | +Logger.log.addOutput(networkOutput); |
| 90 | +Logger.log.addOutput(customOutput); |
| 91 | +
|
| 92 | +// Logs will be sent to console + all added outputs |
| 93 | +Logger.log.i('This goes everywhere!'); |
| 94 | +``` |
| 95 | + |
| 96 | +### Custom Logger Instance |
| 97 | + |
| 98 | +You can create custom logger instances for specific components: |
| 99 | + |
| 100 | +```dart |
| 101 | +final myLogger = NdkLogger( |
| 102 | + level: LogLevel.info, |
| 103 | + outputs: [ |
| 104 | + ConsoleOutput(includeTimestamp: true), |
| 105 | + MyCustomOutput(), |
| 106 | + ], |
| 107 | +); |
| 108 | +
|
| 109 | +myLogger.i('Message from custom logger'); |
| 110 | +``` |
| 111 | + |
| 112 | +## Configuration via NdkConfig |
| 113 | + |
| 114 | +You can set the log level when initializing NDK: |
| 115 | + |
| 116 | +```dart |
| 117 | +final ndk = Ndk(NdkConfig( |
| 118 | + cache: MemCacheManager(), |
| 119 | + eventVerifier: Bip340EventVerifier(), |
| 120 | + logLevel: LogLevel.debug, // Set desired log level |
| 121 | +)); |
| 122 | +``` |
| 123 | + |
| 124 | +## Migration from logger package |
| 125 | + |
| 126 | +If you were using the `logger` package directly, update your code: |
| 127 | + |
| 128 | +**Before:** |
| 129 | +```dart |
| 130 | +import 'package:logger/logger.dart' as lib_logger; |
| 131 | +Logger.setLogLevel(lib_logger.Level.warning); |
| 132 | +``` |
| 133 | + |
| 134 | +**After:** |
| 135 | +```dart |
| 136 | +import 'package:ndk/ndk.dart'; |
| 137 | +Logger.setLogLevel(LogLevel.warning); |
| 138 | +``` |
| 139 | + |
| 140 | +## Example |
| 141 | + |
| 142 | +See [example/logger_example.dart](../../example/logger_example.dart) for a complete example showing: |
| 143 | +- Basic usage |
| 144 | +- Custom outputs |
| 145 | +- Multiple outputs |
| 146 | +- Changing log levels |
| 147 | +- Custom logger instances |
0 commit comments