Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/chirp/lib/src/writers/console_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,21 @@ class PrintConsoleWriter extends ChirpWriter {
///
/// Falls back to [RainbowMessageFormatter] if no [formatter] is provided.
/// Use [output] to redirect logs for testing or alternative destinations.
/// Use [minLevel] to filter out logs below a certain level.
PrintConsoleWriter({
ConsoleMessageFormatter? formatter,
int? maxChunkLength,
TerminalCapabilities? capabilities,
void Function(String)? output,
ChirpLogLevel? minLevel,
}) : formatter = formatter ?? RainbowMessageFormatter(),
maxChunkLength = maxChunkLength ?? platformPrintMaxChunkLength,
capabilities = capabilities ?? TerminalCapabilities.autoDetect(),
output = output ?? print;
output = output ?? print {
if (minLevel != null) {
setMinLogLevel(minLevel);
}
}

@override
bool get requiresCallerInfo => formatter.requiresCallerInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ class DeveloperLogConsoleWriter extends ChirpWriter {
///
/// Use this for development with a debugger attached to see unlimited-length
/// log output. Falls back to [RainbowMessageFormatter] if no [formatter]
/// is provided.
/// is provided. Use [minLevel] to filter out logs below a certain level.
DeveloperLogConsoleWriter({
ConsoleMessageFormatter? formatter,
}) : formatter = formatter ?? RainbowMessageFormatter();
ChirpLogLevel? minLevel,
}) : formatter = formatter ?? RainbowMessageFormatter() {
if (minLevel != null) {
setMinLogLevel(minLevel);
}
}

@override
bool get requiresCallerInfo => formatter.requiresCallerInfo;
Expand Down
18 changes: 17 additions & 1 deletion packages/chirp/lib/src/writers/file_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,16 @@ class JsonFileFormatter implements FileMessageFormatter {
/// );
/// ```
///
/// ## Level Filtering
///
/// ```dart
/// // Only write warnings and above to file
/// final writer = RotatingFileWriter(
/// baseFilePath: '/var/log/errors.log',
/// minLevel: ChirpLogLevel.warning,
/// );
/// ```
///
/// ## File Extensions
///
/// Choose the file extension based on the formatter:
Expand Down Expand Up @@ -465,13 +475,19 @@ class RotatingFileWriter extends ChirpWriter {
/// - [rotationConfig]: Rotation settings, or `null` for no rotation
/// - [encoding]: Text encoding (default: UTF-8)
/// - [onError]: Handler for write failures (default: prints to stderr)
/// - [minLevel]: Minimum log level to write (default: `null` - accept all)
RotatingFileWriter({
required this.baseFilePath,
FileMessageFormatter? formatter,
this.rotationConfig,
this.encoding = utf8,
this.onError,
}) : formatter = formatter ?? const SimpleFileFormatter();
ChirpLogLevel? minLevel,
}) : formatter = formatter ?? const SimpleFileFormatter() {
if (minLevel != null) {
setMinLogLevel(minLevel);
}
}

/// Opens the log file for writing.
///
Expand Down
44 changes: 44 additions & 0 deletions packages/chirp/test/console_writer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,50 @@ void main() {
expect(outputs[0].length, 1000);
});
});

group('PrintConsoleWriter minLevel', () {
test('minLevel constructor parameter sets minLogLevel', () {
final writer = PrintConsoleWriter(
minLevel: ChirpLogLevel.warning,
);
expect(writer.minLogLevel, ChirpLogLevel.warning);
});

test('minLevel null does not set minLogLevel', () {
final writer = PrintConsoleWriter();
expect(writer.minLogLevel, isNull);
});

test('minLevel filters messages when used with logger', () {
final outputs = <String>[];
final writer = PrintConsoleWriter(
formatter: _TestFormatter('test'),
output: outputs.add,
minLevel: ChirpLogLevel.warning,
);

final logger = ChirpLogger(name: 'Test').addWriter(writer);

logger.info('should not appear');
logger.warning('should appear');

expect(outputs.length, 1);
});
});

group('DeveloperLogConsoleWriter minLevel', () {
test('minLevel constructor parameter sets minLogLevel', () {
final writer = DeveloperLogConsoleWriter(
minLevel: ChirpLogLevel.error,
);
expect(writer.minLogLevel, ChirpLogLevel.error);
});

test('minLevel null does not set minLogLevel', () {
final writer = DeveloperLogConsoleWriter();
expect(writer.minLogLevel, isNull);
});
});
}

/// Test formatter that always outputs the given text.
Expand Down
48 changes: 48 additions & 0 deletions packages/chirp/test/file_writer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,54 @@ void main() {
expect(content, contains('WARNING - should appear'));
expect(content, contains('ERROR - should appear'));
});

test('minLevel constructor parameter filters messages', () async {
final tempDir = createTempDir();
final logPath = '${tempDir.path}/app.log';
final writer = RotatingFileWriter(
baseFilePath: logPath,
minLevel: ChirpLogLevel.warning,
);
addTearDown(() => writer.close());

final logger = ChirpLogger(name: 'Test').addWriter(writer);

logger.info('INFO - should be filtered');
logger.debug('DEBUG - should be filtered');
logger.warning('WARNING - should appear');
logger.error('ERROR - should appear');

await writer.flush();

final content = File(logPath).readAsStringSync();
expect(content, isNot(contains('should be filtered')),
reason: 'Messages below warning level should not be written');
expect(content, contains('WARNING - should appear'));
expect(content, contains('ERROR - should appear'));
});

test('minLevel sets minLogLevel property', () {
final tempDir = createTempDir();
final logPath = '${tempDir.path}/app.log';
final writer = RotatingFileWriter(
baseFilePath: logPath,
minLevel: ChirpLogLevel.error,
);
addTearDown(() => writer.close());

expect(writer.minLogLevel, ChirpLogLevel.error,
reason: 'minLevel should set the minLogLevel property');
});

test('minLevel null does not set minLogLevel', () {
final tempDir = createTempDir();
final logPath = '${tempDir.path}/app.log';
final writer = RotatingFileWriter(baseFilePath: logPath);
addTearDown(() => writer.close());

expect(writer.minLogLevel, isNull,
reason: 'minLogLevel should be null by default');
});
});

group('Size-based rotation', () {
Expand Down