Skip to content

Commit 8fd3381

Browse files
Merged logger into single unified module.
1 parent fb5b93e commit 8fd3381

File tree

10 files changed

+83
-243
lines changed

10 files changed

+83
-243
lines changed

src/common/log/log.module.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/common/log/log.service.ts

Lines changed: 0 additions & 139 deletions
This file was deleted.

src/common/log/models/console.transport.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/common/log/models/log-error-meta.model.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/common/log/models/log-meta.model.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/common/logger/colors.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Shared color and formatting utilities for logger
2+
export const colors = {
3+
reset: '\x1b[0m',
4+
bright: '\x1b[1m',
5+
dim: '\x1b[2m',
6+
red: '\x1b[31m',
7+
green: '\x1b[32m',
8+
yellow: '\x1b[33m',
9+
blue: '\x1b[34m',
10+
magenta: '\x1b[35m',
11+
cyan: '\x1b[36m',
12+
white: '\x1b[37m',
13+
gray: '\x1b[90m',
14+
} as const;
15+
16+
export function colorize(level: string, text: string): string {
17+
switch (level) {
18+
case 'info':
19+
return `${colors.blue}[INFO]${colors.reset} ${text}`;
20+
case 'warn':
21+
return `${colors.yellow}[WARN]${colors.reset} ${text}`;
22+
case 'error':
23+
return `${colors.red}[ERROR]${colors.reset} ${text}`;
24+
case 'success':
25+
return `${colors.green}[SUCCESS]${colors.reset} ${text}`;
26+
case 'debug':
27+
return `${colors.gray}[DEBUG]${colors.reset} ${text}`;
28+
default:
29+
return text;
30+
}
31+
}
32+
33+
export function formatLog(level: string, message: string, context?: string, ...args: any[]): [string, ...any[]] {
34+
const now = new Date().toISOString();
35+
const ctx = context ? `${colors.magenta}[${context}]${colors.reset} ` : '';
36+
return [`${colors.gray}[${now}]${colors.reset} ${colorize(level, ctx + message)}`, ...args];
37+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Module, RequestMethod, MiddlewareConsumer, NestModule } from '@nestjs/common';
1+
import { Module, MiddlewareConsumer, RequestMethod, NestModule } from '@nestjs/common';
2+
import { LoggerService } from './logger.service';
23
import { requestLoggingMiddleware } from './middleware';
34

45
@Module({
5-
imports: [],
6-
controllers: [],
7-
providers: [],
6+
providers: [LoggerService],
7+
exports: [LoggerService],
88
})
9-
export class LoggerModule implements NestModule {
9+
export class CommonLoggerModule implements NestModule {
1010
configure(consumer: MiddlewareConsumer): void {
1111
consumer
1212
.apply(requestLoggingMiddleware)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Injectable, Scope, Inject, Optional } from '@nestjs/common';
2+
import { formatLog, colors } from './colors';
3+
4+
/**
5+
* Injectable logger service for context-aware logging.
6+
* Usage: inject LoggerService and call .info(), .warn(), etc.
7+
*/
8+
@Injectable({ scope: Scope.TRANSIENT })
9+
export class LoggerService {
10+
private context?: string;
11+
12+
constructor(@Optional() @Inject('LOGGER_CONTEXT') context?: string) {
13+
if (context) this.context = context;
14+
}
15+
16+
setContext(context: string) {
17+
this.context = context;
18+
}
19+
20+
info(message: string, ...args: any[]) {
21+
console.log(...formatLog('info', message, this.context, ...args));
22+
}
23+
warn(message: string, ...args: any[]) {
24+
console.log(...formatLog('warn', message, this.context, ...args));
25+
}
26+
error(message: string, ...args: any[]) {
27+
console.log(...formatLog('error', message, this.context, ...args));
28+
}
29+
debug(message: string, ...args: any[]) {
30+
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'local') {
31+
console.log(...formatLog('debug', message, this.context, ...args));
32+
}
33+
}
34+
success(message: string, ...args: any[]) {
35+
console.log(...formatLog('success', message, this.context, ...args));
36+
}
37+
38+
// Expose colors for advanced use
39+
colors = colors;
40+
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Request, Response, NextFunction } from 'express';
2-
import { colors } from './logger';
2+
import { colors } from './colors';
33

4-
// Common noise paths that shouldn't produce logs
54
const IGNORED_PATHS = new Set<string>([
65
'/favicon.ico',
76
'/robots.txt',
@@ -17,7 +16,6 @@ function isIgnoredPath(urlOrPath: string): boolean {
1716
const path = (urlOrPath || '').split('?')[0];
1817
if (!path) return false;
1918
if (IGNORED_PATHS.has(path)) return true;
20-
// Also ignore well-known browser icon probes
2119
if (path.startsWith('/favicon')) return true;
2220
return false;
2321
}
@@ -73,7 +71,6 @@ export function requestLoggingMiddleware(req: Request, res: Response, next: Next
7371
const url = req.originalUrl || req.url;
7472
const clientIP = getClientIP(req);
7573

76-
// Skip logging for ignored paths
7774
if (isIgnoredPath(url)) {
7875
return next();
7976
}

src/logger/logger.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)