Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added winston logger starter files: #261

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions backend/logger/developmentLogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { format, createLogger, transports } = require('winston');
const { timestamp, combine, printf, errors } = format;

const developmentLogger = () => {
const logFormat = printf(({ level, message, timestamp, stack }) => {
return `${timestamp} ${level}: ${stack || message}`;
});

return createLogger({
level: 'info',
format: combine(
format.colorize(),
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
errors({ stack: true }),
logFormat
),
transports: [new transports.Console()],
});
}

module.exports = developmentLogger;
18 changes: 18 additions & 0 deletions backend/logger/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// imports
const productionLogger = require('./productionLogger.js');
const testLogger = require('./testLogger.js');
const developmentLogger = require('./developmentLogger.js');

// logger
let logger = null;

// set logger based on environment
if (process.env.NODE_ENV === 'production') {
logger = productionLogger();
} else if (process.env.NODE_ENV === 'test') {
logger = testLogger();
} else {
logger = developmentLogger();
}

module.exports = logger;
22 changes: 22 additions & 0 deletions backend/logger/productionLogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require('path');
const { format, createLogger, transports } = require('winston');
const { timestamp, combine, errors, json } = format;

const productionLogger = () => {
return createLogger({
level: 'warning',
format: combine(timestamp(), errors({ stack: true }), json()),
transports: [
new transports.File({
filename: path.join(__dirname, 'logs', 'combined.log'),
level: 'info'
}),
new transports.File({
filename: path.join(__dirname, 'logs', 'errors.log'),
level: 'error'
}),
],
});
}

module.exports = productionLogger;
7 changes: 7 additions & 0 deletions backend/logger/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const logger = require('./index.js');

logger.info('Hello world');
logger.warn('Warning message');
logger.error('Error message');
logger.debug('Debugging info');
logger.error(new Error('Error object'));
32 changes: 32 additions & 0 deletions backend/logger/testLogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const path = require('path');
const { format, createLogger, transports } = require('winston');
const { timestamp, combine, printf, errors } = format;

const testLogger = () => {
const logFormat = printf(({ level, message, timestamp, stack }) => {
return `${timestamp} ${level}: ${stack || message}`;
});

return createLogger({
level: 'error',
format: combine(
format.colorize(),
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
errors({ stack: true }),
logFormat
),
transports: [
new transports.File({
filename: path.join(__dirname, 'testLogs', 'combined.log'),
level: 'info'
}),
new transports.File({
filename: path.join(__dirname, 'testLogs', 'errors.log'),
level: 'error'
}),
new transports.Console(),
],
});
}

module.exports = testLogger;
Loading