Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Created September 7, 2023 23:31
Show Gist options
  • Save tzkmx/2ce37d5e7bd913787c12e987410d687c to your computer and use it in GitHub Desktop.
Save tzkmx/2ce37d5e7bd913787c12e987410d687c to your computer and use it in GitHub Desktop.
Example of logger using DI with awilix

Notable example of Dependency Injection and use of FileTransports discussed in awilix issues

jeffijoe/awilix#128

'use strict';
import {createContainer, asClass, asFunction, Lifetime, asValue} from "awilix";
const container = createContainer();
container.loadModules( [
[`${__dirname}/system/*.js`, {
injector: () => ({ params : {
mechanism : 'local'
}
})
}]
], {
formatName: 'camelCase',
resolverOptions: {
register: asFunction,
lifetime: Lifetime.SINGLETON
}
});
example
const log = container.cradle.systemTracer({
endpoint : '/',
component : 'notification',
service : 'auth',
method : 'http'
}).info({mechanism : 'amqp' , authToken : 'dldksfldkfdslfk', password : 'asldkalasdlsdad',message : 'lklsdkflsdkf'});
'use strict';
import { createLogger, format, transports } from 'winston';
import 'winston-daily-rotate-file';
const {splat, combine, timestamp, label, prettyPrint, json } = format;
const { DailyRotateFile, Console } = transports;
export default ({params}) => ({mechanism=params.mechanism, endpoint, service, component, method}) => {
console.log({mechanism});
const rootPath = `logs/${endpoint}/${service}/${method}`;
// fileTransport to store locally
const fileTransport = new (DailyRotateFile)({
filename: `${component}.log`,
dirname : rootPath,
auditFile : `${rootPath}/${component}-audit.json`,
handleExceptions: (process.env.NODE_ENV !== 'production'),
datePattern: 'YYYY-MM-DD-HH',
zippedArchive: true,
maxSize: process.env.LOG_SIZE || '20m',
maxFiles: process.env.LOG_ROTATION_DAYS || '14d'
});
// check if the log trace is suppose to be muted ( not visible in logs )
const isPrivate = format((info, opts) => {
if (info.private) { return false; }
return info;
});
// add header from the object to the trace. Endpoint, service, component, method
const addPath = format( (info, opts) => {
info.enpoint = endpoint;
info.service = service;
info.component = component;
info.method = method;
return info;
});
// default configuration for the logger
const loggerOptions = {
level : process.env.LOG_LEVEL || 'info',
format : combine(
isPrivate(),
isPassword(),
isToken(),
json()
),
transports: [
fileTransport
],
exceptionHandlers: [
new transports.File({ filename: 'exceptions.log' })
]
};
// create the logger instance
const logger = createLogger(loggerOptions);
// if Dev, then "output" the log in the console too.
// If Prod, then "log" in files + AMQP.
if (process.env.NODE_ENV !== 'production') {
logger.add(new Console({
format: combine(
splat(),
isPrivate(),
addPath(),
timestamp(),
prettyPrint(),
)
}));
} else {
// add AMQP connector or syslog connector... We will see.
}
// Do not cause winston to exit the application.
logger.exitOnError = false;
return logger
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment