Skip to content

Commit 32cd7d1

Browse files
committed
beautify logs
1 parent e57998a commit 32cd7d1

4 files changed

Lines changed: 52 additions & 31 deletions

File tree

src/metrics/mongodb.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import promClient from 'prom-client';
22
import { MongoClient, MongoClientOptions } from 'mongodb';
3+
import { Effect, sgr } from '../utils/ansi';
34

45
/**
56
* MongoDB command duration histogram
@@ -137,16 +138,17 @@ function formatParams(params: any): string {
137138
*/
138139
function logCommandStarted(event: any): void {
139140
const collectionRaw = extractCollectionFromCommand(event.command, event.commandName);
140-
const collection = normalizeCollectionName(collectionRaw);
141+
const collection = sgr(normalizeCollectionName(collectionRaw), Effect.ForegroundGreen);
141142
const db = event.databaseName || 'unknown db';
143+
const commandName = sgr(event.commandName, Effect.ForegroundRed);
142144
const filter = event.command.filter;
143145
const update = event.command.update;
144146
const pipeline = event.command.pipeline;
145147
const projection = event.command.projection;
146148
const params = filter || update || pipeline;
147149
const paramsStr = formatParams(params);
148150

149-
console.log(`[${event.requestId}] ${db}.${collection}.${event.commandName}(${paramsStr}) ${projection ? `projection: ${formatParams(projection)}` : ''}`);
151+
console.log(`[${event.requestId}] ${db}.${collection}.${commandName}(${paramsStr}) ${projection ? `projection: ${formatParams(projection)}` : ''}`);
150152
}
151153

152154
/**

src/utils/ansi.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* ANSI escape codes for text formatting
3+
*/
4+
export enum Effect {
5+
Reset = '\x1b[0m',
6+
Bold = '\x1b[1m',
7+
Underline = '\x1b[4m',
8+
CrossedOut = '\x1b[9m',
9+
BoldOff = '\x1b[22m',
10+
UnderlineOff = '\x1b[24m',
11+
CrossedOutOff = '\x1b[29m',
12+
ForegroundRed = '\x1b[31m',
13+
ForegroundGreen = '\x1b[32m',
14+
ForegroundYellow = '\x1b[33m',
15+
ForegroundBlue = '\x1b[34m',
16+
ForegroundMagenta = '\x1b[35m',
17+
ForegroundCyan = '\x1b[36m',
18+
ForegroundWhite = '\x1b[37m',
19+
ForegroundGray = '\x1b[90m',
20+
BackgroundRed = '\x1b[41m',
21+
BackgroundGreen = '\x1b[42m',
22+
BackgroundYellow = '\x1b[43m',
23+
BackgroundBlue = '\x1b[44m',
24+
BackgroundMagenta = '\x1b[45m',
25+
BackgroundCyan = '\x1b[46m',
26+
BackgroundWhite = '\x1b[47m',
27+
BackgroundGray = '\x1b[100m',
28+
};
29+
30+
/**
31+
* ANSI escape code for setting visual effects using SGR (Select Graphic Rendition) subset
32+
*
33+
* @example console.log('Hello, ${sgr('world', Effect.ForegroundRed)}');
34+
*
35+
*
36+
* @param message - The message to colorize
37+
* @param color - The color to apply
38+
* @returns The colored message
39+
*/
40+
export function sgr(message: string, color: Effect | Effect[]): string {
41+
const colorCode = Array.isArray(color) ? color.join('') : color ?? Effect.Reset;
42+
43+
return `${colorCode}${message}${Effect.Reset}`;
44+
}

src/utils/logger.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import morgan from 'morgan';
22
import express from 'express';
3+
import { sgr, Effect } from './ansi';
34

45
/**
56
* Setup custom GraphQL-aware morgan token.
@@ -14,7 +15,7 @@ morgan.token('graphql-operation', (req: express.Request) => {
1415
const match = req.body.query.match(/(?:query|mutation)\s+(\w+)/);
1516

1617
if (match && match[1]) {
17-
return match[1];
18+
return sgr(sgr(match[1], Effect.ForegroundMagenta), Effect.Bold);
1819
}
1920
}
2021

@@ -27,8 +28,8 @@ morgan.token('graphql-operation', (req: express.Request) => {
2728
* Production: Apache-style format with operation name included
2829
*/
2930
const customFormat = process.env.NODE_ENV === 'production'
30-
? ':remote-addr - :remote-user [:date[clf]] ":method :url :graphql-operation" :status :res[content-length] - :response-time ms'
31-
: ':method :url :graphql-operation :status :response-time ms - :res[content-length]';
31+
? ':remote-addr - :remote-user [:date[clf]] ":method :url :graphql-operation" :status :res[content-length] bytes - :response-time ms'
32+
: ':method :url :graphql-operation :status :res[content-length] bytes - :response-time ms';
3233

3334
/**
3435
* Configured morgan middleware with GraphQL operation name logging

src/utils/utils.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,5 @@
11
const _ = require('lodash');
22

3-
module.exports.deepMerge = deepMerge;
4-
/**
5-
* Merge to objects recursively
6-
* @param {object} target
7-
* @param {object[]} sources
8-
* @return {object}
9-
*/
10-
function deepMerge(target, ...sources) {
11-
const isObject = (item) => item && typeOf(item) === 'object';
12-
13-
return _.mergeWith({}, target, ...sources, function (_subject, _target) {
14-
if (_.isArray(_subject) && _.isArray(_target)) {
15-
const biggerArray = _subject.length > _target.length ? _subject : _target;
16-
const lesser = _subject.length > _target.length ? _target : _subject;
17-
18-
return biggerArray.map((el, i) => {
19-
if (isObject(el) && isObject(lesser[i])) {
20-
return _.mergeWith({}, el, lesser[i]);
21-
} else {
22-
return el;
23-
}
24-
});
25-
}
26-
});
27-
}
28-
293
/**
304
* Returns real type of passed variable
315
* @param obj

0 commit comments

Comments
 (0)