분류, 패키지 및 저장소 검색

GitHub 저장소dart-archive/logging

logging

디버그 및 오류 로깅을 위한 Dart 패키지입니다.

logging 프로젝트 이미지
스타
326
포크
49
최근 푸시(UTC)
2024. 10. 18.
프로젝트 상태
보관됨

원본 README

아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.

[!IMPORTANT]
This repo has moved to https://github.com/dart-lang/core/tree/main/pkgs/logging

Build Status Pub package publisher

Initializing

By default, the logging package does not do anything useful with the log messages. You must configure the logging level and add a handler for the log messages.

Here is a simple logging configuration that logs all messages via print.

Logger.root.level = Level.ALL; // defaults to Level.INFO
Logger.root.onRecord.listen((record) {
  print('${record.level.name}: ${record.time}: ${record.message}');
});

First, set the root Level. All messages at or above the current level are sent to the onRecord stream. Available levels are:

  • Level.OFF
  • Level.SHOUT
  • Level.SEVERE
  • Level.WARNING
  • Level.INFO
  • Level.CONFIG
  • Level.FINE
  • Level.FINER
  • Level.FINEST

Then, listen on the onRecord stream for LogRecord events. The LogRecord class has various properties for the message, error, logger name, and more.

To listen for changed level notifications use:

Logger.root.onLevelChanged.listen((level) {
  print('The new log level is $level');
});

Logging messages

Create a Logger with a unique name to easily identify the source of the log messages.

final log = Logger('MyClassName');

Here is an example of logging a debug message and an error:

var future = doSomethingAsync().then((result) {
  log.fine('Got the result: $result');
  processResult(result);
}).catchError((e, stackTrace) => log.severe('Oh noes!', e, stackTrace));

When logging more complex messages, you can pass a closure instead that will be evaluated only if the message is actually logged:

log.fine(() => [1, 2, 3, 4, 5].map((e) => e * 4).join("-"));

Available logging methods are:

  • log.shout(logged_content);
  • log.severe(logged_content);
  • log.warning(logged_content);
  • log.info(logged_content);
  • log.config(logged_content);
  • log.fine(logged_content);
  • log.finer(logged_content);
  • log.finest(logged_content);

Configuration

Loggers can be individually configured and listened to. When an individual logger has no specific configuration, it uses the configuration and any listeners found at Logger.root.

To begin, set the global boolean hierarchicalLoggingEnabled to true.

Then, create unique loggers and configure their level attributes and assign any listeners to their onRecord streams.

  hierarchicalLoggingEnabled = true;
  Logger.root.level = Level.WARNING;
  Logger.root.onRecord.listen((record) {
    print('[ROOT][WARNING+] ${record.message}');
  });

  final log1 = Logger('FINE+');
  log1.level = Level.FINE;
  log1.onRecord.listen((record) {
    print('[LOG1][FINE+] ${record.message}');
  });

  // log2 inherits LEVEL value of WARNING from `Logger.root`
  final log2 = Logger('WARNING+');
  log2.onRecord.listen((record) {
    print('[LOG2][WARNING+] ${record.message}');
  });


  // Will NOT print because FINER is too low level for `Logger.root`.
  log1.finer('LOG_01 FINER (X)');

  // Will print twice ([LOG1] & [ROOT])
  log1.fine('LOG_01 FINE (√√)');

  // Will print ONCE because `log1` only uses root listener.
  log1.warning('LOG_01 WARNING (√)');

  // Will never print because FINE is too low level.
  log2.fine('LOG_02 FINE (X)');

  // Will print twice ([LOG2] & [ROOT]) because warning is sufficient for all
  // loggers' levels.
  log2.warning('LOG_02 WARNING (√√)');

  // Will never print because `info` is filtered by `Logger.root.level` of
  // `Level.WARNING`.
  log2.info('INFO (X)');

Results in:

[LOG1][FINE+] LOG_01 FINE (√√)
[ROOT][WARNING+] LOG_01 FINE (√√)
[LOG1][FINE+] LOG_01 WARNING (√)
[ROOT][WARNING+] LOG_01 WARNING (√)
[LOG2][WARNING+] LOG_02 WARNING (√√)
[ROOT][WARNING+] LOG_02 WARNING (√√)

Publishing automation

For information about our publishing automation and release process, see https://github.com/dart-lang/ecosystem/wiki/Publishing-automation.

Dart Archive GitHub avatar
GitHub 작성자

Dart Archive ↗

Legacy projects kept around for posterity – See github.com/dart-lang for current work

언어Dart

사용 중인 의존성

의존성 목록 2 개

  • dart_flutter_team_lints개발 의존성^3.0.0
  • test개발 의존성^1.16.0

프로젝트에 맞는 저장소를 선택하려면?

사용 목적에 따라 검색하고 개발 활동을 비교한 뒤 원본 저장소에서 요구 사항을 확인하세요.

  1. 이름, 작성자 또는 설명으로 검색하고 기술 주제로 범위를 좁힙니다.
  2. 스타, 포크, 최근 푸시를 함께 비교하세요. 스타는 관심의 지표이며 품질을 보장하지 않습니다.
  3. 사용하기 전에 GitHub에서 README, 라이선스, Issues를 확인하세요.

출처 및 관리

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗