fp_logger
A simple, pretty-printing logger for Flutter and Dart, with built-in Dio and GraphQL request/response logging and automatic redaction of sensitive data (passwords, tokens, card numbers, and more) before anything is printed.
For more details, visit logger.
Features
- Readable, boxed console output with log level, timestamp and tag
Logger.d/i/w/e/slog levels, plusLogger.boxed,Logger.rawandLogger.json- Automatic redaction of sensitive keys (passwords, tokens, API keys, card
numbers, etc.) in any logged
Map/List, on by default DioLogger- a DioInterceptorthat logs requests, responses and errorsGraphqlDioLogger- a DioInterceptorfor GraphQL requests over DioLoggerLink- agql_linkLinkfor GraphQL requests made viagraphql/gql_linkinstead of Dio- Truncation of large bodies via
LoggerOptions.maxBodyLength - A global
Logger.enabledswitch andLogger.globalRedactflag - Safe by default in release builds - only
info,warninganderrorlevels are logged in release mode
Installation
dependencies:
fp_logger: ^3.2.0
Basic logging
import 'package:fp_logger/fp_logger.dart';
Logger.d('Debug message', tag: 'MAIN');
Logger.i('Info message');
Logger.w('Something looks off');
Logger.e('Something failed', error: error, stackTrace: stackTrace);
Logger.s('Operation succeeded');
// A boxed group of related lines
Logger.boxed(['Step 1 done', 'Step 2 done'], tag: 'FLOW');
// Pretty-prints a Map/List with redaction applied
Logger.json({'userId': 1, 'password': 'secret'});
// Logs without box formatting - useful for large payloads; disabled in
// release builds
Logger.raw('raw line');
Any Map or List you log is redacted automatically before printing:
Logger.i({'username': 'john', 'password': 'super-secret'});
// │ {
// │ "username": "john",
// │ "password": "***[REDACTED]***"
// │ }
Redaction
Redaction is on by default and recognizes common sensitive key names out of
the box (password, token, secret, apiKey, cardNumber, cvv, ssn,
and many more - including variants like newPassword or X-Api-Key).
// Disable/enable redaction globally
Logger.globalRedact = false;
// Add your own sensitive keys
Logger.addSensitiveKeys(['customToken', 'internalId']);
Logger.removeSensitiveKeys(['internalId']);
Logger.isSensitiveKey('apiKey'); // true
// Redact arbitrary data yourself
final redacted = Logger.redactData({'password': '123'});
// Override redaction for a single call
Logger.i(data, redact: false);
Muting the logger
// Silence all logging - e.g. for tests, or to gate logging behind a
// runtime setting
Logger.enabled = false;
Logging Dio requests
import 'package:dio/dio.dart';
import 'package:fp_logger/fp_logger.dart';
final dio = Dio()
..interceptors.add(
const DioLogger(
loggerOptions: LoggerOptions(
requestHeader: true,
requestBody: true,
responseHeader: false,
responseBody: true,
authHeader: false, // strip Authorization from logs by default
redact: true,
maxBodyLength: 2000, // truncate large bodies instead of dumping them in full
),
),
);
GraphqlDioLogger takes the same LoggerOptions and logs the GraphQL
operation name, query, variables and response/errors instead of a generic
body:
final dio = Dio()..interceptors.add(const GraphqlDioLogger());
Logging GraphQL requests via gql_link
If you're using graphql/gql_link directly (not through Dio), add
LoggerLink to your link chain:
import 'package:gql_link/gql_link.dart';
import 'package:fp_logger/fp_logger.dart';
final link = Link.from([
const LoggerLink(options: LoggerOptions(requestBody: true, responseBody: true)),
httpLink,
]);
LoggerOptions
| Option | Default | Description |
|---|---|---|
authHeader |
false |
Whether to log the Authorization header |
requestBody |
true |
Whether to log the request body |
requestHeader |
false |
Whether to log request headers |
responseBody |
true |
Whether to log the response body |
responseHeader |
false |
Whether to log response headers |
error |
true |
Whether to log errors |
redact |
true |
Whether to redact sensitive data for this logger |
maxBodyLength |
null |
Truncates a logged body past this many characters |
LoggerOptions also exposes copyWith to derive variants of a shared config:
const base = LoggerOptions(requestHeader: true);
final verbose = base.copyWith(responseHeader: true, maxBodyLength: 5000);
Libraries
- fp_logger
- A library contains Logger and DioLogger to log network requests and responses for dio http client