Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
feat: add default properties for logger, ignore these in pino-pretty …
Browse files Browse the repository at this point in the history
…output
  • Loading branch information
mariusbegby committed Aug 25, 2023
1 parent 4d9353d commit 5531699
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./src/index.js",
"start-pretty": "node ./src/index.js | pino-pretty",
"start-pretty": "node ./src/index.js | pino-pretty --ignore environment,source,module,name,context,err,error",
"start-pretty-no-ignore": "node ./src/index.js | pino-pretty",
"deploy": "node ./src/utils/deploySlashCommands.js",
"deploy-pretty": "node ./src/utils/deploySlashCommands.js | pino-pretty",
"deploy-pretty": "node ./src/utils/deploySlashCommands.js | pino-pretty --ignore environment,source,module,name,context,err,error",
"eslint": "eslint ./"
},
"dependencies": {
Expand Down
17 changes: 13 additions & 4 deletions src/services/logger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const pino = require('pino');
const config = require('config');

// Retrieve logger options from config
const loggerOptions = config.get('loggerOptions');

const targets = [
Expand Down Expand Up @@ -42,6 +44,7 @@ const targets = [
}
];

// Check for Loki credentials and add Loki as a target if present
if (process.env.LOKI_AUTH_PASSWORD && process.env.LOKI_AUTH_USERNAME) {
targets.push({
target: 'pino-loki',
Expand All @@ -50,8 +53,7 @@ if (process.env.LOKI_AUTH_PASSWORD && process.env.LOKI_AUTH_USERNAME) {
sync: false,
batching: false,
interval: 5,

host: 'https://logs-prod-025.grafana.net',
host: process.env.LOKI_HOST || 'http://localhost:3100',
basicAuth: {
username: process.env.LOKI_AUTH_USERNAME || '',
password: process.env.LOKI_AUTH_PASSWORD || ''
Expand All @@ -62,10 +64,17 @@ if (process.env.LOKI_AUTH_PASSWORD && process.env.LOKI_AUTH_USERNAME) {

const transport = pino.transport({ targets });

// Default properties for the logger, these will be added to every log entry
const defaultProperties = {
environment: process.env.NODE_ENV || 'development'
};

const logLevelConfig = {
level: loggerOptions.minimumLogLevel,
timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`,
base: undefined
base: defaultProperties
};

module.exports = pino(logLevelConfig, transport);
const logger = pino(logLevelConfig, transport);

module.exports = logger;

0 comments on commit 5531699

Please sign in to comment.