@involves/logger-lib

    2.0.14 • Public • Published

    Logger library and middleware for Node.JS

    Build status

    Install

    npm install @involves/logger-lib
    

    Usage - Logger

    Basic example how to log with the library:

    const { logger } = require('@involves/logger-lib');
    
    (() => {
      logger.info('Logger example')
    })()

    Outputs:

    {"message":"Logger example","level":"info","timestamp":"2021-03-22T18:10:22.362Z"}
    

    You can configure your logger to add more details:

    const packageJson = require('./package.json')
    const { LoggerConfigure, logger } = require('@involves/logger-lib');
    
    (() => {
      LoggerConfigure({ name: packageJson.name, version: packageJson.version }, process.env.LOG_LEVEL)
      logger.info('Logger example')
    })()

    Outputs:

    {"message":"Logger example","level":"info","program":{"name":"importer-engine-file-uploader","version":"1.0.0"},"hostname":"inv001029","timestamp":"2021-03-22T18:13:53.470Z"}
    

    Usage - Middleware

    Just add the middleware into express:

    const app = require('express')()
    const { LoggerMiddleware } = require('@involves/logger-lib')
    
    app.use(LoggerMiddleware.logRequest)

    Example of request log (formatted):

    {
        "message": "Request log",
        "org": {},
        "req": {
            "method": "GET",
            "host": "127.0.0.1",
            "url": "/v1/healthz",
            "headers": {
                "host": "127.0.0.1:37081",
                "accept-encoding": "gzip, deflate",
                "user-agent": "node-superagent/3.8.3",
                "connection": "close"
            }
        },
        "res": {
            "status": 200,
            "headers": {},
            "elapsedTime": 55
        },
        "level": "info",
        "program": {
            "name": "importer-engine-file-uploader",
            "version": "1.0.0"
        },
        "hostname": "64ea6afaa0ff",
        "timestamp": "2021-03-22T18:16:20.231Z"
    }

    JSON Schema

    {
        "timestamp": "timestamp which the log entry was emitted, must be from UTC",
        "hostname": "name of the host which the program",
        "level": "string representation of the log level (trace|debug|info|warn|error|fatal)",
        "message": "descriptive message of the log entry",
        "traceId": "unique identifier to enable traceability",
        "org": {
            "clientId": "client unique identifier",
            "userId": "user unique identifier",
            "environmentId": "environment unique identifier",
            "applicationId": "application unique identifier"
        },
        "program": {
            "name": "name of the program",
            "version": "unique identifier of the program being executed"
        },
        "req": {
            "id": "request unique identifier",
            "method": "http method",
            "scheme": "scheme of the request",
            "host": "host of the request",
            "url": "request url with path, query and fragments",
            "headers": {
                "name of the header in lowercase": "value of the header in lowercase"
            },
            "body": "string representation of the request body",
            "ip": "IP address of the client that initiates the request"
        },
        "res": {
            "status": "http status code",
            "headers": {
                "name of the header in lowercase": "value of the header in lowercase"
            },
            "body": "string representation of the response body",
            "bodyByteLength": "byte length of the response body",
            "elapsedTime": "time duration to process the request"
        },
        "error": {
            "message": "error message",
            "stacktrace": "error stacktrace"
        }
    }

    Keywords

    Install

    npm i @involves/logger-lib

    DownloadsWeekly Downloads

    63

    Version

    2.0.14

    License

    ISC

    Unpacked Size

    20.1 kB

    Total Files

    13

    Last publish

    Collaborators

    • dbbd
    • involvesos
    • iotbernardes
    • marcos.morelli