1.3.0 • Published 7 years ago

@slab/logging v1.3.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

@slab/logging

A flexible logger library for Angular.

Installation

First you need to install the npm module:

npm install @slab/logging --save

Usage

1. Import the LoggingModule

You can use @slab/logging in your Angular project. You have to import LoggingModule.forRoot() in the root NgModule of your application.

The forRoot static method is a convention that provides and configures services at the same time. Make sure you only call this method in the root module of your application, most of the time called AppModule. This method allows you to configure the LoggingModule by specifying a loader.

import {NgModule} from '@angular/core';
import {LoggingModule} from '@slab/logging';

@NgModule({
    imports: [
        LoggingModule.forRoot()
    ]
})
export class AppModule { }
SharedModule

If you use a SharedModule that you import in multiple other feature modules, you can export the LoggingModule to make sure you don't have to import it in every module.

@NgModule({
    exports: [
        TranslateModule
    ]
})
export class SharedModule { }

Note: Never call a forRoot static method in the SharedModule. You might end up with different instances of the service in your injector tree. But you can use forChild if necessary.

Lazy loaded modules

When you lazy load a module, you should use the forChild static method to import the LoggingModule.

Since lazy loaded modules use a different injector from the rest of your application, you can configure them separately with a different logger. By default, it will share its data with other instances of the service (but you can still use a different logger).

@NgModule({
    imports: [
        LoggingModule.forChild({
            level: LogLevel.Info,
            loader: { provide: Logger, useClass: FakeLogger }
        })
    ]
})
export class LazyLoadedModule { }
Configuration

By default, there is no logger available. You can write your own logger, or import an existing one. For example you can use the ConsoleLogger that will log using console.

To use it, you need to install the logger-console package from @slab:

npm install @slab/logger-console --save

Once you've decided which loader to use, you have to setup the LoggingModule to use it.

Here is how you would use the ConsoleLogger:

import {NgModule} from '@angular/core';
import {LoggingModule, Logger, LogService} from '@slab/logging';
import {ConsoleLogger} from '@slab/logger-console';

@NgModule({
    imports: [
        LoggingModule.forRoot({
            level: LogLevel.Info
            logger: { 
              provide: Logger, 
              useFactory: (logService: LogService) {
                return new ConsoleLogger(logService); 
              },
              deps: [LogService]
            }
        })
    ]
})
export class AppModule { }
1.3.0

7 years ago

1.2.3

7 years ago

1.1.3

7 years ago

1.2.2

8 years ago

1.1.2

8 years ago

1.1.0

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.0

8 years ago