Skip to content

Commit

Permalink
Adds logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tc-mccarthy committed Feb 13, 2024
1 parent 95d7a7a commit ba47314
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ services:
- BUILD_VERSION=local-build # DO NOT SPECIFY WHEN DEPLOYING TO STAGE
- NODE_ENV=${ENV} # CAN ONLY BE 'local' OR 'production', hardcode to 'production' WHEN DEPLOYING TO STAGE AND PROD
- NODE_OPTIONS=--enable-source-maps
- LOG_LEVEL=info
- LOG_PRETTY=on
logging:
driver: "json-file"
options:
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import express from 'express';
import cookieParser from 'cookie-parser';
import logger from 'morgan';
// import logger from "morgan";
import apiRouter from './routes/api';
import api_headers from './lib/api_headers';
import api_errors from './lib/api_errors';
import logger from './lib/logger';

const app = express();

app.use(logger('dev'));
app.use(logger.info);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
Expand All @@ -21,7 +22,7 @@ app.get('/', (req, res, next) => {
});

app.listen(3000, () => {
console.info('Service running on port 3000');
logger.info('Service running on port 3000');
});

export default app;
20 changes: 20 additions & 0 deletions lib/logger.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import winston from 'winston';

const log_format = [
winston.format.errors({ stack: true }),
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.json()
];

if (process.env.LOG_PRETTY === 'on') {
log_format.push(winston.format.prettyPrint());
}

const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: winston.format.combine(...log_format),
defaultMeta: { hostname: process.env.HOSTNAME || 'localhost' },
transports: [new winston.transports.Console()]
});

export default logger;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"debug": "^2.6.9",
"express": "^4.16.1",
"http-errors": "^2.0.0",
"morgan": "^1.9.1"
"morgan": "^1.9.1",
"winston": "^3.11.0"
},
"devDependencies": {
"@babel/core": "^7.23.9",
Expand Down
10 changes: 5 additions & 5 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
module: {
rules: [
{
test: /\.(js)$/,
test: /\.(m|c)?(js)$/,
exclude: /node_modules/,
use: ['babel-loader']
}
Expand All @@ -39,7 +39,7 @@ module.exports = {
minimize: process.env.ENV !== 'local',
minimizer: [
new TerserPlugin({
test: /\.(js)$/,
test: /\.(m|c)?(js)$/,
extractComments: true,
parallel: true,
terserOptions: {
Expand All @@ -53,7 +53,7 @@ module.exports = {
plugins: [
new ESLintLoader({
fix: true,
files: ['**/*.js']
files: ['**/*.js', '**/*.mjs', '**/*.cjs']
}),
new NodemonPlugin({
// If using more than one entry, you can specify
Expand All @@ -73,7 +73,7 @@ module.exports = {
ignore: ['*.js.map'],

// Extensions to watch.
ext: 'js,njk,json',
ext: 'js,njk,json,mjs,cjs',

// Unlike the cli option, delay here is in milliseconds (also note that it's a string).
// Here's 1 second delay:
Expand All @@ -93,6 +93,6 @@ module.exports = {
})
],
resolve: {
extensions: ['*', '.js']
extensions: ['*', '.js', '.mjs', '.cjs']
}
};
Loading

0 comments on commit ba47314

Please sign in to comment.