Skip to content

Commit 40b3cd1

Browse files
author
Matija Petrunic
committed
Replace winston with pino
1 parent 469b1e7 commit 40b3cd1

File tree

11 files changed

+265
-213
lines changed

11 files changed

+265
-213
lines changed

.env.sample

+2-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,5 @@ BACKEND_LOCAL_PORT=3000
1919
DB_CONTAINER_NAME=fastify-starter-db
2020
DB_LOCAL_PORT=5432
2121
METRICS_IP_WHITELIST=*
22-
# Winston-loki default url is empty
23-
# WINSTON_LOKI_URL=
24-
# WINSTON_LOKI_BASIC_AUTH=
25-
# WINSTON_LOKI_APP_NAME=myApp
26-
# WINSTON_LOKI_BATCHING=false
22+
# By default prints to stdout
23+
# LOG_FILE = <file_location>

docker-compose.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ services:
1313
- .:/usr/app
1414
- node_modules:/usr/app/node_modules
1515
ports:
16-
- "${BACKEND_LOCAL_PORT:-3000}:${SERVER_PORT:-3000}"
16+
- "${BACKEND_LOCAL_PORT:-3001}:${SERVER_PORT:-3000}"
1717
- "${DEBUG_PORT:-56745}:56745"
1818
db:
1919
image: postgres:9.6-alpine
@@ -29,4 +29,3 @@ services:
2929
volumes:
3030
node_modules:
3131
postgres:
32-

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@
5454
"node-cleanup": "^2.1.2",
5555
"pg": "^8.5.1",
5656
"pg-native": "^3.0.0",
57+
"pino": "^6.11.3",
58+
"pino-pretty": "^5.0.1",
5759
"typeorm": "^0.2.30",
58-
"typeorm-seeding": "^1.6.1",
59-
"winston-loki": "6.0.0-rc.9"
60+
"typeorm-seeding": "^1.6.1"
6061
},
6162
"devDependencies": {
6263
"@types/chai": "^4.2.14",
6364
"@types/faker": "^5.1.5",
65+
"@types/pino": "^6.3.8",
66+
"@types/pino-pretty": "^4.7.0",
6467
"@types/mocha": "^8.2.0",
6568
"@types/node": "^14.14.22",
6669
"@types/node-cleanup": "^2.1.1",

src/App.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { Connection } from "typeorm";
1414
import { config as envPluginConfig } from "./config";
1515
import { getDatabaseConnection } from "./services/db";
1616
import { logger } from "./services/logger";
17-
import { fastifyLogger } from "./services/logger/fastify";
1817
import { routesPlugin } from "./services/plugins/routes";
1918
import { SWAGGER_CONFIG } from "./services/swagger";
2019
export class App {
@@ -30,7 +29,7 @@ export class App {
3029
*/
3130
public static async init(): Promise<App> {
3231
const instance = fastify({
33-
logger: fastifyLogger,
32+
logger: logger,
3433
return503OnClosing: true,
3534
});
3635
const app = new App(instance);

src/controllers/sample.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { SampleRepository } from "../repositories/sample";
22
import { ApiController } from "../services/fastify-types";
3-
import { logger } from "../services/logger";
43

54
interface GetQuery {
65
name?: string;
76
}
87

98
export const get: ApiController<GetQuery> = {
109
handler: async function (request, reply) {
11-
logger.info("Fetching samples", { requestId: request.id });
10+
request.log.info("Fetching samples");
11+
1212
const sampleRepository = this.db.getCustomRepository(SampleRepository);
1313
if (request.query.name) {
1414
reply.send(await sampleRepository.findByName(request.query.name));

src/services/logger/fastify.ts

-96
This file was deleted.

src/services/logger/index.ts

+13-49
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,17 @@
1-
import winston from "@nodefactory/winston";
2-
import LokiTransport from "winston-loki";
3-
import Transports from "winston-transport";
1+
import pino from "pino";
42

5-
const format = winston.format.printf(
6-
({ level, message, labels, timestamp, requestId }) => {
7-
message = winston.format
8-
.colorize({ all: false, message: true })
9-
.colorize(level, message);
10-
let log = `${timestamp} [${
11-
labels.module
12-
}] ${level.toUpperCase()}: ${message}`;
13-
if (requestId) {
14-
log += " RequestId: " + requestId;
15-
}
16-
return log;
17-
}
18-
);
3+
export let logger: pino.Logger;
194

20-
const transportsConfig: Transports[] = [
21-
new winston.transports.Console({
22-
format: winston.format.combine(
23-
winston.format.timestamp(),
24-
winston.format.align(),
25-
format
26-
),
27-
}),
28-
];
29-
30-
if (process.env.WINSTON_LOKI_URL) {
31-
transportsConfig.push(
32-
new LokiTransport({
33-
host: process.env.WINSTON_LOKI_URL,
34-
batching: process.env.WINSTON_LOKI_BATCHING == "true",
35-
basicAuth: process.env.WINSTON_LOKI_BASIC_AUTH ?? undefined,
36-
labels: {
37-
app: process.env.WINSTON_LOKI_APP_NAME ?? "",
38-
},
39-
})
5+
if (process.env.LOG_FILE) {
6+
logger = pino({
7+
level: process.env.LOG_LEVEL || "debug",
8+
});
9+
} else {
10+
logger = pino(
11+
{
12+
level: process.env.LOG_LEVEL || "debug",
13+
prettyPrint: true,
14+
},
15+
process.stdout
4016
);
4117
}
42-
43-
export const logger = winston.createLogger({
44-
level: process.env.LOG_LEVEL || "debug",
45-
46-
format: winston.format.json({}),
47-
defaultMeta: {
48-
labels: {
49-
module: "default",
50-
},
51-
},
52-
transports: transportsConfig,
53-
});

src/services/logger/typeorm.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Logger as WinstonLogger } from "@nodefactory/winston";
1+
import pino from "pino";
22
import { Logger } from "typeorm";
33

44
import { logger } from "./index";
55

66
export class TypeOrmLogger implements Logger {
7-
private readonly logger: WinstonLogger;
7+
private readonly logger: pino.Logger;
88

99
constructor() {
1010
this.logger = logger.child({ label: "database" });

test/e2e/sample.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { app } from "./app-setup";
66

77
describe("Sample e2e test", function () {
88
beforeEach(async function () {
9-
logger.silent = true;
9+
logger.level = "silent";
1010
});
1111

1212
afterEach(async function () {
13-
logger.silent = false;
13+
logger.level = "silent";
1414
});
1515

1616
it("Should successfully query database", async function () {

test/unit/controllers/sample.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe("sample controller", function () {
1515
let sampleRepositoryStub: SinonStubbedInstance<SampleRepository>;
1616

1717
beforeEach(async function () {
18-
logger.silent = true;
18+
logger.level = "silent";
1919
app = await App.init();
2020
sampleRepositoryStub = sinon.createStubInstance(SampleRepository);
2121
app.instance.decorate("db", {
@@ -24,7 +24,7 @@ describe("sample controller", function () {
2424
});
2525

2626
afterEach(async function () {
27-
logger.silent = false;
27+
logger.level = "silent";
2828
});
2929

3030
it("get samples", async function () {

0 commit comments

Comments
 (0)