Skip to content

Commit 148a25a

Browse files
committed
added logging mechanism
1 parent d4526f5 commit 148a25a

File tree

5 files changed

+229
-2
lines changed

5 files changed

+229
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.env
22
.vscode/
3+
logs
34
client/.next/
45
node_modules/
56
client/config.js

package-lock.json

+158
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@sentry/apm": "^5.21.1",
3939
"@sentry/node": "^5.21.1",
4040
"@sentry/react": "^5.21.1",
41+
"app-root-path": "^3.0.0",
4142
"axios": "^0.21.1",
4243
"babel-plugin-inline-react-svg": "^1.1.0",
4344
"bcryptjs": "^2.4.3",
@@ -95,7 +96,8 @@
9596
"url-regex": "^4.1.1",
9697
"use-media": "^1.4.0",
9798
"useragent": "^2.2.1",
98-
"uuid": "^3.4.0"
99+
"uuid": "^3.4.0",
100+
"winston": "^3.3.3"
99101
},
100102
"devDependencies": {
101103
"@babel/cli": "^7.8.3",

server/config/winston.ts

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import appRoot from "app-root-path";
2+
import winston from "winston";
3+
4+
const { combine, colorize, printf, timestamp } = winston.format;
5+
6+
const logFormat = printf(info => {
7+
return `[${info.timestamp}] ${info.level}: ${info.message}`;
8+
});
9+
10+
const rawFormat = printf(info => {
11+
return `[${info.timestamp}] ${info.level}: ${info.message}`;
12+
});
13+
14+
// define the custom settings for each transport (file, console)
15+
const options = {
16+
file: {
17+
level: "info",
18+
filename: `${appRoot}/logs/app.log`,
19+
handleExceptions: true,
20+
json: true,
21+
maxsize: 5242880, // 5MB
22+
maxFiles: 5,
23+
colorize: false
24+
},
25+
errorFile: {
26+
level: "error",
27+
name: "file.error",
28+
filename: `${appRoot}/logs/error.log`,
29+
handleExceptions: true,
30+
json: true,
31+
maxsize: 5242880, // 5MB
32+
maxFiles: 100,
33+
colorize: true
34+
},
35+
console: {
36+
level: "debug",
37+
handleExceptions: true,
38+
json: false,
39+
format: combine(colorize(), rawFormat)
40+
}
41+
};
42+
43+
// instantiate a new Winston Logger with the settings defined above
44+
export const logger = winston.createLogger({
45+
format: combine(timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), logFormat),
46+
transports: [
47+
new winston.transports.File(options.file),
48+
new winston.transports.Console(options.console)
49+
],
50+
exitOnError: false // do not exit on handled exceptions
51+
});
52+
53+
// create a stream object with a 'write' function that will be used by `morgan`
54+
export const stream = {
55+
write: message => {
56+
logger.info(message);
57+
}
58+
};
59+
60+
winston.addColors({
61+
debug: "white",
62+
error: "red",
63+
info: "green",
64+
warn: "yellow"
65+
});

server/server.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as links from "./handlers/links";
1414
import * as auth from "./handlers/auth";
1515
import __v1Routes from "./__v1";
1616
import routes from "./routes";
17+
import { stream } from "./config/winston";
1718

1819
import "./cron";
1920
import "./passport";
@@ -28,7 +29,7 @@ app.prepare().then(async () => {
2829
server.set("trust proxy", true);
2930

3031
if (env.isDev) {
31-
server.use(morgan("dev"));
32+
server.use(morgan("combined", { stream }));
3233
} else if (env.SENTRY_PRIVATE_DSN) {
3334
Sentry.init({
3435
dsn: env.SENTRY_PRIVATE_DSN,

0 commit comments

Comments
 (0)