Skip to content

Commit

Permalink
feat: logger files(#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkgbunny committed May 1, 2019
1 parent 61412f2 commit 42eece4
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 2 deletions.
2 changes: 2 additions & 0 deletions expressEngine/logger/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import logger from './logger';
export { logger };
135 changes: 135 additions & 0 deletions expressEngine/logger/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// import winston
import { createLogger, format, transports } from 'winston';

const logger = () => {
const {
combine, timestamp, json, colorize, simple,
} = format;

// setup logger
const error = createLogger({
level: 'error',
format: combine(
timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
json(),
),
defaultMeta: { service: 'user-service' },
transports: [
new transports.Console({
format: combine(colorize(), simple()),
}),
new transports.File({
filename: 'logs/error.log',
level: 'error',
}),
],
});

const warn = createLogger({
level: 'warn',
format: combine(
timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
json(),
),
defaultMeta: { service: 'user-service' },
transports: [
new transports.Console({
format: combine(colorize(), simple()),
}),
new transports.File({
filename: 'logs/warn.log',
level: 'warn',
}),
],
});

const info = createLogger({
level: 'info',
format: combine(
timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
json(),
),
defaultMeta: { service: 'user-service' },
transports: [
new transports.Console({
format: combine(colorize(), simple()),
}),
new transports.File({
filename: 'logs/info.log',
level: 'info',
}),
],
});

const verbose = createLogger({
level: 'verbose',
format: combine(
timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
json(),
),
defaultMeta: { service: 'user-service' },
transports: [
new transports.Console({
format: combine(colorize(), simple()),
}),
new transports.File({
filename: 'logs/verbose.log',
level: 'verbose',
}),
],
});

const debug = createLogger({
level: 'debug',
format: combine(
timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
json(),
),
defaultMeta: { service: 'user-service' },
transports: [
new transports.Console({
format: combine(colorize(), simple()),
}),
new transports.File({
filename: 'logs/debug.log',
level: 'debug',
}),
],
});

const silly = createLogger({
level: 'silly',
format: combine(
timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
json(),
),
defaultMeta: { service: 'user-service' },
transports: [
new transports.Console({
format: combine(colorize(), simple()),
}),
new transports.File({
filename: 'logs/silly.log',
level: 'silly',
}),
],
});
const winstonLogger = {
error, warn, info, verbose, debug, silly,
};
return winstonLogger;
};

export default logger;
1 change: 1 addition & 0 deletions expressEngine/logs/debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"This is a debug message","level":"debug","service":"user-service","timestamp":"2019-05-01 16:37:39"}
1 change: 1 addition & 0 deletions expressEngine/logs/error.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"This is an error message","level":"error","service":"user-service","timestamp":"2019-05-01 16:37:39"}
2 changes: 2 additions & 0 deletions expressEngine/logs/info.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"message":"Db connected successfully","level":"info","service":"user-service","timestamp":"2019-05-01 16:37:39"}
{"message":"App running on port 5000","level":"info","service":"user-service","timestamp":"2019-05-01 16:37:39"}
Empty file added expressEngine/logs/silly.log
Empty file.
Empty file added expressEngine/logs/verbose.log
Empty file.
1 change: 1 addition & 0 deletions expressEngine/logs/warn.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"This is a warning message","level":"warn","service":"user-service","timestamp":"2019-05-01 16:37:39"}
3 changes: 2 additions & 1 deletion expressEngine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"express": "^4.16.4",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.4.12",
"swagger-ui-express": "^4.0.2"
"swagger-ui-express": "^4.0.2",
"winston": "^3.2.1"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
Expand Down
12 changes: 11 additions & 1 deletion expressEngine/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import express from 'express';
import bodyParser from 'body-parser';
import { config } from 'dotenv';
import { logger } from './logger';

// import db methods
import db from './db/db';
Expand All @@ -12,6 +13,9 @@ import Route from './route';

import seed from './db/seedData';

// Blocking console.log()
console.log = function () {};

// Set up the express app
const app = express();
config();
Expand All @@ -26,12 +30,18 @@ const port = PORT || 5000;
db.open().then(async () => {
try {
console.log('Db connected successfully');
logger().info.info('Db connected successfully');
logger().warn.warn('This is a warning message');
logger().debug.debug('This is a debug message');
logger().error.error('This is an error message');
seed();
await app.listen(port);
console.log(`App running on port ${port}`);
logger().info.info(`App running on port ${port}`);
} catch (err) {
console.warn(err);
logger().warn.warn(err);
}
}).catch((err) => {
console.warn(err);
logger().warn.warn(err);
});

0 comments on commit 42eece4

Please sign in to comment.