Created
May 26, 2022 09:12
-
-
Save MohammedALREAI/346f3bf95dea4767ef2ffdabc9b89056 to your computer and use it in GitHub Desktop.
CustomLooger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs'; | |
import config from 'config'; | |
import path from 'path'; | |
import winston, { | |
createLogger, | |
LoggerOptions, | |
transports, | |
format | |
} from 'winston'; | |
import winstonDaily from 'winston-daily-rotate-file'; | |
import expressWinston from 'express-winston'; | |
import pino from 'pino'; | |
const logFormat = winston.format.printf( | |
({ timestamp, level, message }) => `${timestamp} ${level}: ${message}` | |
); | |
// logs dir | |
const logDir: string = path.join(__dirname, '../../logs'); | |
if (!fs.existsSync(logDir)) { | |
fs.mkdirSync(logDir); | |
} | |
const config: LoggerOptions = { | |
format: winston.format.combine( | |
winston.format.timestamp({ | |
format: 'YYYY-MM-DD HH:mm:ss' | |
}), | |
winston.format.errors({ stack: true }), | |
winston.format.label({ label: `Label🏷️` }), | |
winston.format.splat(), | |
winston.format.simple(), | |
logFormat | |
), | |
transports: [ | |
new winstonDaily({ | |
level: 'debug', | |
datePattern: 'YYYY-MM-DD', | |
dirname: logDir + '/debug', // log file /logs/debug/*.log in save | |
filename: `%DATE%.log`, | |
maxFiles: 30, // 30 Days saved | |
json: false, | |
zippedArchive: true | |
}), | |
// error log setting | |
new winstonDaily({ | |
level: 'error', | |
datePattern: 'YYYY-MM-DD', | |
dirname: logDir + '/error', // log file /logs/error/*.log in save | |
filename: `%DATE%.log`, | |
maxFiles: 30, // 30 Days saved | |
handleExceptions: true, | |
json: false, | |
zippedArchive: true | |
}), | |
new winstonDaily({ | |
level: 'info', | |
datePattern: 'YYYY-MM-DD', | |
dirname: logDir + '/info', // log file /logs/error/*.log in save | |
filename: `%DATE%.log`, | |
maxFiles: 30, // 30 Days saved | |
handleExceptions: true, | |
json: false, | |
zippedArchive: true | |
}) | |
] | |
}; | |
class Logger { | |
private logger: winston.Logger; | |
constructor() { | |
this.logger = createLogger(config); | |
if (process.env.ENV !== 'production') { | |
this.logger.add( | |
new transports.Console({ | |
format: format.combine(format.colorize(), format.simple()) | |
}) | |
); | |
} | |
} | |
public log(level?: string, ...msg: any[]) { | |
this.logger.log(level, msg); | |
} | |
public getRequestLogger() { | |
return expressWinston.logger({ | |
transports: [new winston.transports.Console()], | |
format: winston.format.combine( | |
winston.format.colorize(), | |
winston.format.simple() | |
), | |
meta: process.env.ENV !== 'production', // | |
msg: 'HTTP {{req.method}} {{req.url}}', // | |
expressFormat: true, // Use the default Express/ | |
colorize: false, // Color the text and status | |
ignoreRoute(req, res) { | |
return false; | |
} | |
}); | |
} | |
public getRequestErrorLogger() { | |
return expressWinston.errorLogger({ | |
transports: [new winston.transports.Console()], | |
format: winston.format.combine( | |
winston.format.colorize(), | |
winston.format.simple() | |
) | |
}); | |
} | |
} | |
export { Logger }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IN APP OR SERVER
in app.ts or server.ts
try to made instace for class
static logger: Logger = new Logger();
setup your initializeMiddleware -->
that for the handle to longer your request.
this.app.use(AppServer.logger.getRequestLogger());
used it