From 254b40b251be7b3432514b6913463ec3b85fff91 Mon Sep 17 00:00:00 2001 From: Anxo Rodriguez Date: Fri, 2 Aug 2024 18:04:07 +0100 Subject: [PATCH] Make logs pretty for development --- .env.example | 1 + apps/api/src/main.ts | 19 ++++++++++++++++--- package.json | 1 + yarn.lock | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 6c9b499..c587b0d 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,5 @@ # Version: Used in the /about endpoint. Useful to expose the Docker image version. +LOG_LEVEL=info VESION=1.0.0 # Port diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 723a700..83111d0 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -1,14 +1,27 @@ import Fastify from 'fastify'; import { app } from './app/app'; +import pino from 'pino'; + +const loggerConfigEnv = + process.env.NODE_ENV === 'production' + ? {} + : { + transport: { + target: 'pino-pretty', + }, + }; + +const logger = pino({ + ...loggerConfigEnv, + level: process.env.LOG_LEVEL ?? 'info', +}); const host = process.env.HOST ?? 'localhost'; const port = process.env.PORT ? Number(process.env.PORT) : 3000; // Instantiate Fastify with some config export const server = Fastify({ - logger: { - level: process.env.LOG_LEVEL ?? 'info', - }, + logger, }); // Register your application as a normal plugin. diff --git a/package.json b/package.json index f75944a..3ad6ac1 100644 --- a/package.json +++ b/package.json @@ -91,6 +91,7 @@ "jest-environment-node": "^29.4.1", "nx": "16.3.2", "openapi-typescript": "^7.0.2", + "pino-pretty": "^11.2.2", "prettier": "^2.6.2", "ts-jest": "^29.1.0", "ts-node": "10.9.1", diff --git a/yarn.lock b/yarn.lock index bdb4695..f2ada16 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4849,6 +4849,11 @@ fast-copy@^3.0.0: resolved "https://registry.npmjs.org/fast-copy/-/fast-copy-3.0.1.tgz" integrity sha512-Knr7NOtK3HWRYGtHoJrjkaWepqT8thIVGAwt0p0aUs1zqkAzXZV4vo9fFNwyb5fcqK1GKYFYxldQdIDVKhUAfA== +fast-copy@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/fast-copy/-/fast-copy-3.0.2.tgz#59c68f59ccbcac82050ba992e0d5c389097c9d35" + integrity sha512-dl0O9Vhju8IrcLndv2eU4ldt1ftXMqqfgN4H1cpmGV7P6jeB9FwpN9a2c8DPGE1Ys88rNUJVYDHq73CGAGOPfQ== + fast-decode-uri-component@^1.0.1: version "1.0.1" resolved "https://registry.npmjs.org/fast-decode-uri-component/-/fast-decode-uri-component-1.0.1.tgz" @@ -5498,6 +5503,11 @@ help-me@^4.0.1: glob "^8.0.0" readable-stream "^3.6.0" +help-me@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/help-me/-/help-me-5.0.0.tgz#b1ebe63b967b74060027c2ac61f9be12d354a6f6" + integrity sha512-7xgomUX6ADmcYzFik0HzAxh/73YlKR9bmFzf51CZwR+b6YtzU2m0u49hQCqV6SvlqIqsaxovfwdvbnsw3b/zpg== + highlight.js@^10.7.1: version "10.7.3" resolved "https://registry.npmjs.org/highlight.js/-/highlight.js-10.7.3.tgz" @@ -7236,6 +7246,26 @@ pino-abstract-transport@^1.0.0, pino-abstract-transport@v1.0.0: readable-stream "^4.0.0" split2 "^4.0.0" +pino-pretty@^11.2.2: + version "11.2.2" + resolved "https://registry.yarnpkg.com/pino-pretty/-/pino-pretty-11.2.2.tgz#5e8ec69b31e90eb187715af07b1d29a544e60d39" + integrity sha512-2FnyGir8nAJAqD3srROdrF1J5BIcMT4nwj7hHSc60El6Uxlym00UbCCd8pYIterstVBFlMyF1yFV8XdGIPbj4A== + dependencies: + colorette "^2.0.7" + dateformat "^4.6.3" + fast-copy "^3.0.2" + fast-safe-stringify "^2.1.1" + help-me "^5.0.0" + joycon "^3.1.1" + minimist "^1.2.6" + on-exit-leak-free "^2.1.0" + pino-abstract-transport "^1.0.0" + pump "^3.0.0" + readable-stream "^4.0.0" + secure-json-parse "^2.4.0" + sonic-boom "^4.0.1" + strip-json-comments "^3.1.1" + pino-pretty@^9.0.0: version "9.4.1" resolved "https://registry.npmjs.org/pino-pretty/-/pino-pretty-9.4.1.tgz" @@ -7901,6 +7931,13 @@ sonic-boom@^3.0.0, sonic-boom@^3.1.0: dependencies: atomic-sleep "^1.0.0" +sonic-boom@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/sonic-boom/-/sonic-boom-4.0.1.tgz#515b7cef2c9290cb362c4536388ddeece07aed30" + integrity sha512-hTSD/6JMLyT4r9zeof6UtuBDpjJ9sO08/nmS5djaA9eozT9oOlNdpXSnzcgj4FTqpk3nkLrs61l4gip9r1HCrQ== + dependencies: + atomic-sleep "^1.0.0" + source-map-js@^1.0.2: version "1.0.2" resolved "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz"