@furystack/logging

7.0.0 • Public • Published

@furystack/logging

Logging package for FuryStack

Initializing with @furystack/inject

You can start using the Logging service with an injector in the following way:

import { ConsoleLogger } from '@furystack/logging'

const myInjector = new Injector().useLogging(ConsoleLogger, Logger1, Logger2 /** ...your Logger implementations */)

You can retrieve the Logger instance with

const myLogger = myInjector.getLogger()

...or with a specific scope:

myInjector.getLogger().withScope('CustomScope')

Logging events

You can log a simple event with

myLogger.addEntry({
  level: 'verbose',
  message: 'My log message',
  scope: '@furystack/logging/test',
  data: {
    foo: 1,
    bar: 42,
  },
})

or

myLogger.verbose({
  message: 'My log message',
  scope: '@furystack/logging/test',
  data: {
    foo: 1,
    bar: 42,
  },
})

The two snippets do the same - they will add a log entry to each registered logger.

Scoped loggers

At the most of the cases, you use a logger in a service with a specific scope. You can create and use a scoped logger in the following way

const scopedLogger = myLogger.withScope('@furystack/logging/test')
scopedLogger.verbose({ message: 'FooBarBaz' })

Implementing your own logger

You can implement your own logging logic in the similar way as this custom log collector

import { AbstractLogger, ILeveledLogEntry } from '@furystack/logging'

@Injectable({ lifetime: 'singleton' })
export class MyCustomLogCollector extends AbstractLogger {
  private readonly entries: Array<ILeveledLogEntry<any>> = []

  public getEntries() {
    return [...this.entries]
  }

  public async addEntry<T>(entry: ILeveledLogEntry<T>): Promise<void> {
    this.entries.push(entry)
  }

  constructor() {
    super()
  }
}

Readme

Keywords

Package Sidebar

Install

npm i @furystack/logging

Weekly Downloads

10

Version

7.0.0

License

GPL-2.0

Unpacked Size

110 kB

Total Files

49

Last publish

Collaborators

  • gallayl