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

feature/24 #139

Closed
wants to merge 1 commit into from
Closed
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
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);
});