logging_service 3.4.0

  • Readme
  • Changelog
  • Installing
  • 57

pub Build Status codecov documentation

The service for advanced work with logging #


You start using logging package. You are able to change log-level for the whole application only. Or you should set log level manually for every new Logger instance inside the code. It is not very convenient.
We wanted to change log-levels per module, and change log-levels per module and for the whole app in runtime. With this library you will be able to do it, even a little bit more - read below.

The fast start example:

final loggingService = new LoggingService()..start();

  appVersionUid: 'app_uid', 
  sentryDsn: 'https://123456789abcdef123456789abcdef12@sentry.local/1'

loggingService.runProtected(() {
  //Your own app code
  var logger1 = new Logger('myModuleName1');
  logger1.info('Some info');
  var logger2 = new Logger('myModuleName2');
  logger2.severe('Some error! We should fix it!');
  logger2.shout('Some dev-message');

What do you get as a result (each feature can be disabled by configuration):

  1. You can manage log level per module:
    By code: LoggingService.setLogLevelPerLogger(String loggerName, log.Level level)
    In runtime by adding the hash parameter to your app-url: #logging={"myModuleName": "SEVERE"} myModuleName should be the name that you used when you created new Logger('myModuleName');

By default you will see in the browser console only Level.SEVERE and higher levels of messages. But if you want to change it you can manage the base level for the whole application:
By code: LoggingService.setLogLevelPerLogger(LoggingService.ROOT_LOG_LEVEL_KEY, log.Level level)
or to set it with the call of: loggingService.start(rootLogLevel: Level.INFO)
In runtime by adding the hash parameter to your app-url: #logging={"rootLogLevel": "INFO"}

  1. You SEVERE messages are being saved to the sentry server: If you put the appVersionUid and sentryDsn arguments to the ConfigureLoggingForBrowser.setUpAll() call.
    Or if you configured saver manually loggingService.addLoggingSavers([new LoggingSaverForSentry(...)]);
    You can change the level for what message is being sent to the savers by changing the savableLogLevel arg for the loggingService.start(savableLogLevel: Level.SEVERE) call.

  2. The default printer is printing messages and exceptions in accordance with your levels-settings. It prints exceptions in a way that allows the browser to use source-maps and show you source-code links if you had connected the source-map file.

  3. The exceptions inside the loggingService.runProtected() call will be captured and chained by default.
    It means that you will see the whole sequence of calls (and their stack-traces) that led to the error.
    Yes, I now that there are some fears that chaining is slow. But you will see it only if you use long nested sequences of call. Very long, like 100-1000 levels. If the size of you sequence is something like 4-10 as usual - the chaining of stack-traces is not your bottleneck, even not a problem. We checked it for us and use it in production.

  4. The system listens js-errors (through window.onError) and handles them.

  5. The system can collect even js-errors that had happened before the dart-code started:
    To do this you should put js-code-snippet from the lib/pre_start_js_collector_code.js to the very beginning of your scripts.

  6. If you want to collect errors from the dart-angular you should use something like this:

import 'package:angular2/di.dart';
import 'package:logging/logging.dart' as log;
import 'package:logging_service/logging_service.dart';
import 'package:stack_trace/stack_trace.dart';

class AppExceptionHandler implements ExceptionHandler {
  final LoggingService _loggingService;

  AppExceptionHandler(loggingService) : this._loggingService = loggingService;

  void call(dynamic error, [dynamic stackTrace, String reason]) {
        if (stackTrace is Iterable) {
          stackTrace = new Chain((stackTrace as Iterable).map((dynamic trace) => new Trace.parse(trace.toString())));
        } else {
          stackTrace = new Trace.parse(stackTrace.toString());

          new log.LogRecord(
            stackTrace as StackTrace,

and then put the provider for ExceptionHandler to your bootstrap call:

bootstrap(AppComponent, [new Provider(ExceptionHandler, useValue: new AppExceptionHandler(loggingService))]);

Changelog #

3.4.0 #

  • Console output has been improved for Additional info section

3.3.0 #

  • Add new option for enable/disable global handling js errors
  • Global handling of js errors is disabled by default now

3.2.5 #

  • Do not send empty exceptions to Sentry

3.2.4 #

  • Fix cast error when configuring logger via url hash params

3.2.3 #

  • Fixes for the last Dart

3.2.2 #

  • Dart 2 fixes

3.2.1 #

  • Fix README

3.2.0 #

  • Improve display errors experience

3.1.1 #

  • Fix a type error inside the window.onError callback #4

3.1.0 #

  • Add js to dart logging

3.0.3 #

  • Fix the "uses_dynamic_as_bottom" for the Dart-2.0

3.0.2 #

  • Just the version bumping

3.0.1 #

  • Check the ci integration

3.0.0 #

  • The first open-source version

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  logging_service: ^3.4.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:logging_service/logging_service.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 3, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health issues and suggestions

Document public APIs. (-1 points)

61 out of 61 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/configure_logging_for_browser.dart. (-8.63 points)

Analysis of lib/configure_logging_for_browser.dart reported 18 hints, including:

line 19 col 15: Avoid const keyword.

line 25 col 46: Unnecessary new keyword.

line 26 col 47: Unnecessary new keyword.

line 33 col 13: Unnecessary new keyword.

line 52 col 24: Use = to separate a named parameter from its default value.

Fix lib/logging_saver_for_sentry.dart. (-3.45 points)

Analysis of lib/logging_saver_for_sentry.dart reported 7 hints, including:

line 31 col 18: Unnecessary new keyword.

line 80 col 33: Unnecessary new keyword.

line 85 col 9: Unnecessary new keyword.

line 93 col 9: Unnecessary new keyword.

line 96 col 53: Unnecessary new keyword.

Fix lib/logging_service.dart. (-3.45 points)

Analysis of lib/logging_service.dart reported 7 hints, including:

line 41 col 54: Use = to separate a named parameter from its default value.

line 41 col 71: Use = to separate a named parameter from its default value.

line 43 col 25: Unnecessary new keyword.

line 52 col 27: Use = to separate a named parameter from its default value.

line 53 col 30: Use = to separate a named parameter from its default value.

Fix additional 9 files with analysis or formatting issues. (-11.42 points)

Additional issues in the following files:

  • lib/logging_speed.dart (6 hints)
  • lib/logging_printer_for_browser.dart (5 hints)
  • lib/infinite_loop_protector.dart (3 hints)
  • lib/src/configure_js_to_dart_logs_writer.dart (3 hints)
  • lib/logging_environment.dart (2 hints)
  • lib/sentry_pre_save_for_browser.dart (2 hints)
  • lib/protector.dart (1 hint)
  • lib/src/clock.dart (1 hint)
  • lib/src/js_console_proxy.dart (Run dartfmt to format lib/src/js_console_proxy.dart.)

Maintenance suggestions

The package description is too short. (-18 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and logging_service.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.21.1 <3.0.0
js ^0.6.0 0.6.1+1
logging ^0.11.0 0.11.4
sentry_client ^4.0.0 4.3.0
stack_trace ^1.8.0 1.9.3
Transitive dependencies
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.4
path 1.6.4
typed_data 1.1.6
uuid 2.0.4
Dev dependencies
build_runner ^1.7.4
build_test ^0.10.2+1
build_web_compilers ^2.9.0
coverage ^0.13.6
mockito ^3.0.0-beta+1
quiver ^2.0.0+1
test ^1.3.3