๐ Astute Logger
A lightweight, configurable logging package for Flutter with colored console output, persistent file logging with rotation, contextual logging, execution time measurement, structured and free-text sensitive data redaction, custom log sinks, and a built-in Dio HTTP interceptor.
โจ Features
- ๐ Multiple log levels
- ๐ข Debug
- ๐ต Info
- ๐ก Warning
- ๐ด Error
- ๐ฃ Critical
- ๐จ Colored console logs
- ๐ Persistent file logging with an async, serialized write queue
- ๐ Automatic log file rotation (size limit + backup count)
- ๐ Custom log sinks (Crashlytics, Sentry, remote upload, etc.)
- โ๏ธ Configurable logger settings
- ๐ Automatic caller method detection (VM + web fallback)
- ๐ Context-aware logging using Zones
- ๐ Request ID propagation
- ๐ท๏ธ Additional contextual metadata & tags
- ๐ Pretty JSON logging
- ๐ Sensitive data redaction
- ๐ Authorization tokens & Bearer headers
- ๐ Passwords, secrets, API keys, OTPs & CVVs
- ๐ง Email addresses (opt-out via
redactEmails) - ๐ณ Credit card numbers
- ๐งฉ Key-based redaction for nested maps, lists &
extrametadata
- โฑ๏ธ Execution time measurement (sync & async)
- ๐จ Error & stack trace logging
- ๐ Built-in Dio
LoggingInterceptorfor HTTP request/response/error traffic - ๐ฑ Cross-platform Flutter support (Android, iOS, Web, Desktop)
๐ฆ Installation
Add the package to your pubspec.yaml.
dependencies:
astute_logger: ^2.7.0
Install it:
flutter pub get
๐ฅ Import
import 'package:astute_logger/astute_logger.dart';
// Optional: HTTP interceptor
import 'package:astute_logger/service/logging_interceptor.dart';
๐๏ธ Create a Logger
final logger = AstuteLogger(
'AppLogger',
config: const LogConfig(
enableConsoleOutput: true,
enableFileOutput: true,
enableColorLogging: true,
enableRedaction: true,
redactEmails: true,
minimumLogLevel: LogLevel.debug,
logFileName: 'app_logs.txt',
maxFileSizeBytes: 2 * 1024 * 1024,
maxBackupFiles: 3,
),
);
โ ๏ธ Logging is disabled in release builds. All log methods return immediately when
kReleaseModeis true, so nothing is printed, written to file, or sent to sinks. Use a sink in your own code if you need release-mode error reporting.
๐ Examples
๐ข Debug
await logger.debug("Debug message");
๐ต Info
await logger.info("Application started");
๐ก Warning
await logger.warning("API response is slow");
๐ด Error
await logger.error(
"Unable to load profile",
error: Exception("HTTP 500"),
stackTrace: StackTrace.current,
);
๐ฃ Critical
await logger.critical(
"Database connection lost",
error: Exception("SocketException"),
stackTrace: StackTrace.current,
);
โน๏ธ Async by default. All write methods (
debug,info,warning,error,critical,json,write) returnFuture<void>. The returned Future completes only after the log has been persisted to disk (if file output is enabled) and delivered to all sinks. Await it whenever you need a durability guarantee.
๐ท๏ธ Logging with Metadata
await logger.info(
"User logged in",
tag: 'AUTH',
extra: {
"userId": 123,
"role": "Admin",
"token": "abc", // โ [REDACTED]
},
);
๐
extravalues go through key-based redaction whenenableRedactionis on.
๐ Context-Aware Logging
await LoggerContext.runWithContext(
requestId: "REQ-1001",
extra: {
"screen": "Home",
"feature": "Login",
},
body: () async {
await logger.info("Inside contextual logging");
// Every log within this Zone automatically carries:
// [ReqID: REQ-1001] [Ctx: {screen: Home, feature: Login}]
},
);
Per-call extra is merged with the Zone extra.
โฑ๏ธ Measure Execution Time
Synchronous
final sorted = logger.logExecutionTime(
"Sorting List",
() {
final list = List.generate(100000, (i) => 100000 - i);
list.sort();
return list;
},
);
The timing log is emitted even if the callback throws. The original exception is rethrown unchanged.
Asynchronous
await logger.logExecutionTimeAsync(
"API Request",
() async {
await Future.delayed(const Duration(seconds: 2));
},
);
๐ Pretty JSON Logging
Use write(prettyPrint: true) for raw JSON strings, or json() for objects.
Pretty-print a JSON string
await logger.write(
message: jsonEncode({
"name": "John",
"age": 25,
"roles": ["Admin", "User"]
}),
prettyPrint: true,
level: LogLevel.info,
);
Log a structured object (auto pretty-printed + redacted)
await logger.json({
"user": "john",
"password": "hunter2", // โ [REDACTED]
"profile": {
"email": "john@example.com", // โ [REDACTED] (key-based)
},
"createdAt": DateTime.now(), // non-encodable values use toString()
});
๐ Pretty-printed JSON goes through both engines: key-based redaction first, then free-text redaction on the final output.
json()respectsenableRedactionand accepts JSON strings as well as maps and lists.
๐ Automatic Sensitive Data Redaction
AstuteLogger offers two complementary redaction engines:
1. Key-based redaction (structured data)
Applied automatically to json(), write(prettyPrint: true), and extra
metadata. It also powers the public redactObject() helper used by the Dio
interceptor.
logger.redactObject({
'user': 'alice',
'credentials': {
'password': 'hunter2',
'token': 'abc',
},
});
// => {
// 'user': 'alice',
// 'credentials': {
// 'password': '[REDACTED]',
// 'token': '[REDACTED]',
// },
// }
Manage the sensitive key set at runtime (keys are case-insensitive):
AstuteLogger.registerSensitiveKey('ssn');
AstuteLogger.unregisterSensitiveKey('ssn');
AstuteLogger.getSensitiveKeys(); // sorted list
Default keys: password, token, accesstoken, refreshtoken,
authorization, apikey, secret, email.
2. Free-text redaction (regex)
Applied to every message when enableRedaction: true.
| Rule | Example input | Output |
|---|---|---|
| Quoted key/value | "password": "p@ss!" |
"password": "[REDACTED]" |
| JSON-style key | {"password":"abc"} |
{"password":"[REDACTED]"} |
| Bearer token | Authorization: Bearer abc-123 |
Authorization: Bearer [REDACTED] |
| Bare key/value | token:abc123, count:5 |
token:[REDACTED], count:5 |
| OTP / CVV | otp=123456, cvv: 123 |
otp=[REDACTED], cvv: [REDACTED] |
| Email address | contact alice@example.com |
contact [REDACTED] |
| Credit card | card 4111111111111111 |
card [REDACTED] |
Keywords matched by the key/value rules: token, password, secret,
api_key / api-key / apikey, otp, cvv, auth, bearer.
Disabling email redaction
If you need to log emails for debugging:
const LogConfig(redactEmails: false)
Emails are preserved, but credit-card numbers and other rules still apply.
To disable redaction entirely, set enableRedaction: false.
๐ Read Log File
final file = await AstuteLogger.getLogFile();
if (file != null && await file.exists()) {
final logs = await file.readAsString();
print(logs);
}
Custom file name:
final file = await AstuteLogger.getLogFile(fileName: 'api_logs.txt');
getLogFile()cachesFileinstances per path to avoid repeated platform channel calls and to keep a stable identity across lookups.
๐ Log File Rotation
When the log file reaches maxFileSizeBytes, it is renamed to a numbered
backup and a fresh file is started:
app_logs.txt โ current
app_logs.1.txt โ newest backup
app_logs.2.txt
app_logs.3.txt โ oldest backup (deleted on next rotation)
const LogConfig(
maxFileSizeBytes: 5 * 1024 * 1024, // rotate at 5 MB
maxBackupFiles: 5, // keep 5 backups
)
maxFileSizeBytes <= 0disables rotation.maxBackupFiles <= 0deletes the old file instead of keeping a backup.- Rotation never throws; a failure will not break logging.
๐ท๏ธ Filter Logs by Tag
final authLogs = await AstuteLogger.getLogsByTag('AUTH');
authLogs.forEach(print);
Custom file name:
final apiLogs = await AstuteLogger.getLogsByTag(
'HTTP',
fileName: 'api_logs.txt',
);
Matching is case-insensitive. Each result is a complete entry, so multi-line logs (stack traces, pretty JSON) stay whole. ANSI color codes are stripped. Tags with special characters such as
A+Bare supported. Only the current file is searched, not rotated backups.
๐ Custom Log Sinks
Send logs anywhere (Crashlytics, Sentry, your own server) by implementing
LogSink:
class CrashlyticsSink implements LogSink {
@override
Future<void> write(LogRecord record) async {
if (record.level.index >= LogLevel.error.index) {
// FirebaseCrashlytics.instance.log(record.message);
}
}
}
final sink = CrashlyticsSink();
AstuteLogger.addSink(sink);
// Later, if needed:
AstuteLogger.removeSink(sink);
LogRecord fields: level, title, tag, message, timestamp, extra,
requestId.
- Sinks run after console and file output, in registration order.
messageandextraare already redacted (when redaction is enabled).- A sink that throws is ignored, so it cannot break logging or other sinks.
- Sinks are global: they receive records from every
AstuteLoggerinstance.
๐ Dio HTTP Logging
Attach the built-in LoggingInterceptor to any Dio instance:
import 'package:dio/dio.dart';
import 'package:astute_logger/service/logging_interceptor.dart';
final dio = Dio()
..interceptors.add(LoggingInterceptor(
logger: logger,
logRequestHeaders: true,
logRequestBody: true,
logResponseHeaders: true,
logResponseBody: true,
maxBodyLength: 2000, // truncate long bodies
maxHeaderValueLength: 500, // truncate long header values
));
What gets logged
- Request: method, URL, headers (sensitive ones redacted), body
- Response: status code + message, elapsed time, headers, body
- Error: status code, elapsed time, URL, request/response headers,
request/response bodies, error message,
DioExceptionType
Redaction & safety
Authorization,Proxy-Authorization,Cookie,Set-Cookie,X-Api-Key,X-Access-Token,Access-Token,Bearer,Tokenheaders are replaced with[REDACTED]. Matching is case-insensitive.- JSON bodies are decoded and passed through
redactObject(), so{"password": "hunter2"}becomes{"password": "[REDACTED]"}. FormDatabodies log only field names and file basenames, never full paths or raw bytes.- Long values are truncated with a
... [truncated, total N chars]suffix. - In release builds the interceptor does no work.
โ๏ธ Configuration
const LogConfig(
enableConsoleOutput: true,
enableFileOutput: true,
enableColorLogging: true,
enableRedaction: true,
redactEmails: true,
minimumLogLevel: LogLevel.debug,
logFileName: 'app_logs.txt',
maxFileSizeBytes: 2 * 1024 * 1024,
maxBackupFiles: 3,
)
| Field | Default | Description |
|---|---|---|
enableRedaction |
true |
Master switch for all redaction |
redactEmails |
true |
Redact email addresses in free text |
minimumLogLevel |
LogLevel.debug |
Drop messages below this level |
enableConsoleOutput |
true |
Write to dart:developer log() |
enableFileOutput |
true |
Persist to the app documents directory (skipped on web) |
logFileName |
'app_logs.txt' |
Target log file name |
enableColorLogging |
true |
ANSI colorize console output |
maxFileSizeBytes |
2097152 (2 MB) |
Rotate the file at this size. <= 0 disables rotation |
maxBackupFiles |
3 |
Number of rotated backups to keep |
๐งญ App Modes
Each log line includes the current Flutter build mode:
logger.appMode; // AppMode.debug | profile | release | unknown
๐งต Threading & Durability
- All log events flow through a single static queue with a bounded capacity (1024 pending events). Producers wait when the queue is full.
- Writes are serialized: events are persisted strictly in enqueue order, even across multiple logger instances writing to the same file.
- File handles are opened and closed per write (
openWrite+close()), so log files are never held open by the process. This is safe on Windows and in tests. - Internal logger errors are caught and reported via
dart:developer(LoggerError). They never surface as unhandled exceptions.
Flush before exit or sharing
// Wait for all queued logs to be written.
await AstuteLogger.flush();
๐งช Testing Hooks
// Clear the internal File cache (useful between tests).
AstuteLogger.resetFileCache();
resetFileCache()is marked@visibleForTesting. Avoid calling it from production code.
โค๏ธ License
MIT License.