Home

Awesome

ngx-action-logger

Description

Logger for Angular applications, capable of both outputting data to the console or sending data to elastic. After sending to the elastic, you can view the data (for example, in kibana), build custom charts, etc. There are two decorators - LogComponentLifeCycle and Log. LogComponentLifeCycle - used for component life cycle logging, Log - function logging. Also, the logger can be used anywhere using DI. It is possible to expand the logged data or use your own service for logging that expands or complements the functionality.

Installation

Add LoggerModule to your AppModule

...
import { appModuleInjector, LoggerModule } from 'ngx-action-logger';
...

@NgModule({
    ...
    imports: [
        ...
        LoggerModule.forRoot({
            sessionId: 'uuid',
            elasticOptions: { index: 'yourIndex', url: 'http://localhost:9200', username: 'elastic', password: '123456' }
        }),
        ...
    ],
    ...
})
export class AppModule {
    constructor(protected injector: Injector) {
        appModuleInjector(injector);
    }
}

LogComponentLifeCycle decorator

@LogComponentLifeCycle({ className: 'UsersComponent', hooks: ['ngOnInit', 'ngOnDestroy'], message: 'customMessage' })
@Component({
    selector: 'app-users',
    templateUrl: './users.component.html',
    styleUrls: ['./users.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class UsersComponent {}

Log decorator

@Log({ className: 'UsersComponent', message: 'customMessage' })
onCancel(): void {}

Kibana example

After successfully sending the data to elastic, you can view the data and build graphs in kibana. For this you need:

  1. Create new index patter (Stack Management -> Index patterns -> Create index pattern). Type your index name to the input and click "Next step".

type your index name

Select time filter filed (index by default contains "timestamp" filed with date property type by default) and click "Create index pattern". select time filter filed

After some seconds you can see your created index pattern with all fields. created index pattern

  1. You can discover index data. Open "Discover" page in kibana. "Discover" page

Select your index pattern and research incoming data. Use search if needed used KQL. "Discover" data

  1. You can combine data views from any Kibana app into one dashboard and see everything in one place. Open "Dashboards" page in kibana. "Dashboards" data

Select "Create new dashboard". Create new dashboard

Add an existing or new object to this dashboard, click "Create new". Create new

Select "Go to Lens" or select visualization. Go to Lens

Drag any fields in left side and drop fields in center for display visualization. Drag any fields

Select any other Suggestions or change settings in right side. Select any Suggestions

Save your visualization and Add title and description. Save your visualization

Create new visualization if needed. Create new visualization if needed.

API

Logger module used API:

LoggerModule

ConsoleLoggerService

Logger by default. Logger use this service when not set other service in config or not set config for elastic. All incoming data in log shown in console

ElasticLoggerService

Send data to elastic for logging. You can check data in kibana or added custom dashboard graphics, for example.

Decorators

LogComponentLifeCycle - life cycle decorator for components.

Log - logging decorator for functions

Injector

Const