nx-logger is a logging utility that grows with the application
- TypeScript 2.9
- Jest unit testing and code coverage
- Type definitions for Node.js v6.x (LTS) and Jest
npm i @raynode/nx-logger
As nxLogger namespace was removed, the access to the internals like interfaces or the log levels was changed. This will result in the following being invalid:
import { nxLogger, create } from '@raynode/nx-logger'
const log = create({ verbosity: nxLogger.DEBUG })
it will be like this now:
import { LogLevel, create } from '@raynode/nx-logger'
const log = create({ verbosity: LogLevel.DEBUG })
Further the nxLogger
is not necessary any more to access the interfaces
// before
import { nxLogger } from '@raynode/nx-logger'
type myLog = nxLogger.Log
// after
import { Log } from '@raynode/nx-logger'
type myLog = Log
// or
import { Log as myLog } from '@raynode/nx-logger'
Basic usage would just render some console output
import { create } from '@raynode/nx-logger'
const log = create('my-project')
log('start-up')
Will run this line of console output
console.log('my-project - start-up')
As I love Debug from visionmedia, there is already a transport to transform your logging.
import { create, configure } from '@raynode/nx-logger'
import { transport } from '@raynode/nx-logger-debug'
configure({ transport })
const log = create('my-project')
log('start-up')
This will now send the log message through debug
, including the namespaces
- Namespaces
- Interchangeable transports
- Verbosity Settings
- Function-Call logging
- Joining and splitting of transports
Each logging instance can have its own namespace. They are designed to be inherited and used per file or per context.
You could configure it per file (with default transport)
// app.ts
configure({ namespace: ['project'] })
const context = create('app')
// Result: project - app : msg
// utils.ts
const context = create('utils')
// Result: project - utils : msg
Or you can use it as per request
configure({ namespace: ['project'] })
const appLog = create('express')
const app = express()
app.use((req, res, next) => {
req.log = appLog(req.url)
next()
})
app.get('/something/*', (req, res) => {
req.log('making')
// Result: project - express - /something/more : making
})
The nx-logger is written in a way that lets you start a project and have simple logging output. And after the project grows you'll only have to attach another transport to send the log output directly to your logging servers or services (like: Loggly).
Like in console
there are some functions to define the verbosity of the logging call.
const context = create({
verbosity: 5, // default
})
context.debug('DEBUG-level: 10')
context.info('INFO-level: 7')
context.log('LOG-level: 5')
context('LOG-level: 5')
context.warn('WARN-level: 3')
context.error('ERROR-level: 1')
The verbosity is set per namespace to more easily debug problems during development.
Sometime we need to know when a specific function is called, and we write code like this:
const myFunction = (a, b) => {
context.log('myFunction', [a, b])
// ...
}
myFunction(1, 2) // myFunction [1, 2]
nx-logger has a logging handler for this case:
const myFunction = context.on((a, b) => {
// ...
}, 'myFunction')
myFunction(1, 2) // myFunction [1, 2]
The context.on(callback, [namespace], [configuration])
feature will enable you to get information whenever the defined function is called.
Combined with the conditions feature it might look like this
const signup = context.on((user: User) => db.run('create', user), 'signup-user', {
transport: join(transport, createTransport(logglyConfiguration)),
})
When a project grows and it is necessary to enable new logging functions, it might be nice to have a possibility to join transports together.
import { create, join } from '@raynode/nx-logger'
import { transport } from '@raynode/nx-logger-debug'
import { createTransport } from '@raynode/nx-logger-loggly'
const context = create({
transport: join(transport, createTransport(logglyConfiguration))
})
context.log('message') // will show up in the console through debug and be transmitted to loggly via loggly-transport.
It might also be good to decide which messages are used in some transport
import { create, split, LogLevel } from '@raynode/nx-logger'
import { transport } from '@raynode/nx-logger-debug'
import { createTransport } from '@raynode/nx-logger-loggly'
const decide = (configuration, messages, verbosity) => {
if(verbosity > LogLevel.LOG)
return true // use debug
else
return false // use loggly for LOG and below
}
const context = create({
transport: split(decide, transport, createTransport(logglyConfiguration))
})
context.log('important') // will be transmitted to loggly via loggly-transport.
context.info('just infos') // will show up in the console through debug
If you want to help with this project, just leave a bugreport or pull request. I'll try to come back to you as soon as possible
MIT