|
| 1 | +import 'package:chirp/src/core/chirp_writer.dart'; |
| 2 | +import 'package:chirp/src/core/log_record.dart'; |
| 3 | +import 'package:chirp/src/utils/stack_trace_util.dart'; |
| 4 | + |
| 5 | +/// {@template chirp.DelegatedChirpWriter} |
| 6 | +/// A [ChirpWriter] implementation that delegates to a function. |
| 7 | +/// |
| 8 | +/// This allows creating writers inline without defining a class: |
| 9 | +/// |
| 10 | +/// ```dart |
| 11 | +/// // Simple writer that collects logs |
| 12 | +/// final logs = <LogRecord>[]; |
| 13 | +/// final collector = DelegatedChirpWriter((record) => logs.add(record)); |
| 14 | +/// |
| 15 | +/// // Writer that sends to external service |
| 16 | +/// final analyticsWriter = DelegatedChirpWriter((record) { |
| 17 | +/// if (record.level >= ChirpLogLevel.error) { |
| 18 | +/// analytics.logError( |
| 19 | +/// message: record.message.toString(), |
| 20 | +/// error: record.error, |
| 21 | +/// stackTrace: record.stackTrace, |
| 22 | +/// ); |
| 23 | +/// } |
| 24 | +/// }); |
| 25 | +/// |
| 26 | +/// // Writer that appends to a file |
| 27 | +/// final fileWriter = DelegatedChirpWriter((record) { |
| 28 | +/// final line = '${record.timestamp.toIso8601String()} ' |
| 29 | +/// '[${record.level.name}] ${record.message}\n'; |
| 30 | +/// logFile.writeAsStringSync(line, mode: FileMode.append); |
| 31 | +/// }); |
| 32 | +/// |
| 33 | +/// // Use with a logger |
| 34 | +/// final logger = ChirpLogger(name: 'MyLogger') |
| 35 | +/// .addWriter(collector) |
| 36 | +/// .addWriter(analyticsWriter); |
| 37 | +/// ``` |
| 38 | +/// |
| 39 | +/// ## Interceptors and Filtering |
| 40 | +/// |
| 41 | +/// [DelegatedChirpWriter] inherits interceptor and minimum log level support |
| 42 | +/// from [ChirpWriter]: |
| 43 | +/// |
| 44 | +/// ```dart |
| 45 | +/// final writer = DelegatedChirpWriter((record) => print(record.message)) |
| 46 | +/// .setMinLogLevel(ChirpLogLevel.warning) |
| 47 | +/// .addInterceptor(myInterceptor); |
| 48 | +/// ``` |
| 49 | +/// |
| 50 | +/// ## Debugging |
| 51 | +/// |
| 52 | +/// By default, the creation site is captured for debugging. This helps |
| 53 | +/// identify which delegated writer is which when inspecting in a debugger: |
| 54 | +/// |
| 55 | +/// ```dart |
| 56 | +/// print(writer); // DelegatedChirpWriter(my_service.dart:42) |
| 57 | +/// ``` |
| 58 | +/// |
| 59 | +/// For more complex writers with state, configuration, or cleanup logic, |
| 60 | +/// consider extending [ChirpWriter] directly. |
| 61 | +/// {@endtemplate} |
| 62 | +class DelegatedChirpWriter extends ChirpWriter { |
| 63 | + /// The function that writes log records. |
| 64 | + final void Function(LogRecord record) _write; |
| 65 | + |
| 66 | + final bool _requiresCallerInfo; |
| 67 | + |
| 68 | + /// Stack trace captured at construction time, for debugging. |
| 69 | + /// |
| 70 | + /// Only captured when assertions are enabled (debug mode). |
| 71 | + final StackTrace? creationStackTrace; |
| 72 | + |
| 73 | + /// {@macro chirp.DelegatedChirpWriter} |
| 74 | + /// |
| 75 | + /// The [write] function receives a [LogRecord] and should output it to the |
| 76 | + /// desired destination (console, file, network, monitoring service, etc.). |
| 77 | + /// |
| 78 | + /// Set [requiresCallerInfo] to `true` if your writer needs access to caller |
| 79 | + /// information (file, line, class, method). This triggers stack trace |
| 80 | + /// capture which has a performance cost. |
| 81 | + /// |
| 82 | + /// **Performance note**: The [write] function is called synchronously for |
| 83 | + /// each log event. For slow operations (network, disk), consider buffering |
| 84 | + /// or handling async operations within your function. |
| 85 | + DelegatedChirpWriter( |
| 86 | + void Function(LogRecord record) write, { |
| 87 | + bool requiresCallerInfo = false, |
| 88 | + }) : _write = write, |
| 89 | + _requiresCallerInfo = requiresCallerInfo, |
| 90 | + creationStackTrace = debugCaptureStackTrace(); |
| 91 | + |
| 92 | + @override |
| 93 | + bool get requiresCallerInfo => _requiresCallerInfo; |
| 94 | + |
| 95 | + @override |
| 96 | + void write(LogRecord record) => _write(record); |
| 97 | + |
| 98 | + @override |
| 99 | + String toString() { |
| 100 | + final stackTrace = creationStackTrace; |
| 101 | + if (stackTrace != null) { |
| 102 | + final info = getCallerInfo(stackTrace); |
| 103 | + if (info != null) { |
| 104 | + return 'DelegatedChirpWriter(${info.callerLocation})'; |
| 105 | + } |
| 106 | + } |
| 107 | + return 'DelegatedChirpWriter'; |
| 108 | + } |
| 109 | +} |
0 commit comments