1.1.6 • Published 9 months ago

logmessage-cls-hooked v1.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Log Message CLS Hooked

npm version

A LogRoot Method Decorator that uses cls-hooked to handle and propagate log message details between different methods deeper in the callstack, removing the need to propagate a paremeter just for logging purposes.

Inspired by Typeorm Transactional CLS Hooked

Installation

npm install logmessage-cls-hooked --save
yarn add logmessage-cls-hooked

Initialization

In order to use it, you will first need to initialize the cls-hooked namespace before your application is started

import { initLogMessageCls } from 'logmessage-cls-hooked';

initLogMessageCls() // Initialize cls-hooked
//...
app = express()
//...

Log Root

A Log root is a context that will receive log properties from all the subsequent methods that are called below it.

Imagine the following scenario:

import { getLogData, LogRoot, setOnLog } from "logmessage-cls-hooked";

class SomeClass {

    @LogRoot()
    sayHi() {
        setOnLog('action', 'hi')
        const language = this.getLanguage()
        getLogData() // action=hi, lang=japanese
    }

    private getLanguage() {
        setOnLog('lang', 'japanese')
        getLogData() // action=hi, lang=japanese
        return 'japanese'
    }

}

If you want to create a new context to add properties without making a mess in the parent context, just add a new LogRoot like that:

import { getLogData, LogRoot, setOnLog } from "logmessage-cls-hooked";

class SomeClass {

    @LogRoot()
    sayHi() {
        setOnLog('action', 'hi')
        const language = this.getLanguage()
        getLogData() // action=hi
    }

    @LogRoot()
    private getLanguage() {
        setOnLog('lang', 'japanese')
        getLogData() // action=hi, lang=japanese
        return 'japanese'
    }

}
1.1.6

9 months ago

1.1.5

10 months ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago