PiedTeam / NikeCloneTraining-BE-Project

0 stars 0 forks source link

[Research] Add server log in BE #138

Open BLonggg608 opened 3 months ago

BLonggg608 commented 3 months ago

cron job kill thread

[[NIKE BE LOG]]


Installing

npm i pino
npm i pino-pretty

Usage

Create logger.ts file in folder utils

In 'logger.ts':

import pino from 'pino'
import { PinoPretty } from 'pino-pretty'

export const logger = pino({
    level: process.env.PINO_LOG_LEVEL || 'info', // Not show events below the PINO_LOG_LEVEL variable or the default level 'info'
    base: {
        pid: false // Disables the pid field in the log output
    },
    transport: {
        target: 'pino-pretty', // Uses the pino-pretty transport for pretty printing logs
        options: {
            colorize: true, // Enables colorized output
            translateTime: 'SYS:yyyy-mm-dd HH:MM:ss' // Formats the time in a human-readable format
        }
    }
})

How to use:

import logger from './path/to/your/logger'

logger.fatal('fatal')   // Critical errors causing application shutdown
logger.error('error')   // Serious issues needing immediate attention
logger.warn('warn')     // Potential problems or non-ideal situations
logger.info('info')     // General operational information
logger.debug('debug')   // Detailed debugging information for developers
logger.trace('trace')   // Extremely detailed tracing of application execution

Output:

Image

Notice: if the level is set to info, debug and trace will not be logged


Log an object with pino:

const user = {
    username: 'John',
    age: 20,
    address: 'USA',
    phone: '0123456789'
}

logger.info({ user }, 'Here is user info!')

Output:

Image


Can read these link for more information: https://betterstack.com/community/guides/logging/how-to-install-setup-and-use-pino-to-log-node-js-applications/

Documentation of Pino: https://getpino.io/#/