Skip to main content

fp_logger 3.2.2 copy "fp_logger: ^3.2.2" to clipboard
fp_logger: ^3.2.2 copied to clipboard

A simple logger for flutter and dart projects. It supports logging to console with formatted messages and stacktrace.

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/s log levels, plus Logger.boxed, Logger.raw and Logger.json
  • Automatic redaction of sensitive keys (passwords, tokens, API keys, card numbers, etc.) in any logged Map/List, on by default
  • DioLogger - a Dio Interceptor that logs requests, responses and errors
  • GraphqlDioLogger - a Dio Interceptor for GraphQL requests over Dio
  • LoggerLink - a gql_link Link for GraphQL requests made via graphql/gql_link instead of Dio
  • Truncation of large bodies via LoggerOptions.maxBodyLength
  • A global Logger.enabled switch and Logger.globalRedact flag
  • Safe by default in release builds - only info, warning and error levels are logged in release mode

Installation #

dependencies:
  fp_logger: ^3.2.1

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());

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);
1
likes
160
points
384
downloads

Documentation

API reference

Publisher

verified publisherkishormainali.com

Weekly Downloads

A simple logger for flutter and dart projects. It supports logging to console with formatted messages and stacktrace.

Homepage
Repository (GitHub)
View/report issues

Topics

#logging #flutter #dart

License

MIT (license)

Dependencies

ansicolor, dio, flutter, gql, gql_exec, gql_link, web

More

Packages that depend on fp_logger