@tgtone/logger
v1.0.3
Published
Debug logger utility for TGT One projects
Readme
@tgtone/logger
Debug logger utility for TGT One projects with environment-based control
Features
- ✅ Environment-aware: Respects
DEBUGandNODE_ENVvariables - ✅ Browser & Node.js compatible: Works in both environments without polyfills
- ✅ Multiple log levels: debug, info, warn, error
- ✅ Namespace support: Organize logs by module/service
- ✅ Production-safe: Auto-disables debug logs in production
- ✅ Zero dependencies: Lightweight and fast
- ✅ TypeScript: Fully typed
- ✅ Vite-ready: No
defineconfig needed
Installation
npm install @tgtone/loggerQuick Start
Basic Usage
import { debug, info, warn, error } from '@tgtone/logger';
debug('🔍 Debug information'); // Only shows if DEBUG=true
info('ℹ️ Important information'); // Always shows
warn('⚠️ Warning message'); // Always shows
error('❌ Error occurred'); // Always showsWith Namespace
import { Logger } from '@tgtone/logger';
const logger = new Logger({ namespace: 'AuthService' });
logger.debug('Token validation started');
// 🔍 [AuthService] Token validation started
logger.info('User logged in:', user.email);
// ℹ️ [AuthService] User logged in: [email protected]Child Loggers
const authLogger = new Logger({ namespace: 'Auth' });
const jwtLogger = authLogger.child('JWT');
jwtLogger.debug('Verifying token');
// 🔍 [Auth:JWT] Verifying tokenConfiguration
Browser Usage (React/Vue/Svelte with Vite)
IMPORTANT: In browser environments, you MUST pass debug explicitly:
import { Logger } from '@tgtone/logger';
// ✅ Correct - pass debug explicitly
const logger = new Logger({
namespace: 'MyApp',
debug: import.meta.env.DEV, // Vite variable for development mode
});
// ❌ Wrong - will default to false in browser
const logger = new Logger({ namespace: 'MyApp' });Why? Browsers don't have access to process.env, so the logger detects the browser environment and requires explicit debug configuration.
Node.js Usage (Backend/NestJS/Express)
In Node.js, environment variables work automatically:
import { Logger } from '@tgtone/logger';
// ✅ Auto-detects DEBUG=true or NODE_ENV=development
const logger = new Logger({ namespace: 'AuthService' });Environment Variables
DEBUG=true- Enable debug logs (Node.js only)NODE_ENV=development- Auto-enable debug logs in development (Node.js only)
Programmatic Config
const logger = new Logger({
debug: true, // Force enable debug
namespace: 'MyService', // Add namespace prefix
emojis: true, // Enable emojis (default: true)
minLevel: LogLevel.INFO, // Minimum log level to show
});Log Levels
import { LogLevel } from '@tgtone/logger';
LogLevel.DEBUG // 🔍 Debug information (only if DEBUG=true)
LogLevel.INFO // ℹ️ General information
LogLevel.WARN // ⚠️ Warnings
LogLevel.ERROR // ❌ ErrorsUse Cases
NestJS Services
import { Logger } from '@tgtone/logger';
export class AuthService {
private logger = new Logger({ namespace: 'AuthService' });
async login(email: string, password: string) {
this.logger.debug('Login attempt:', email);
// ... authentication logic
this.logger.info('User logged in:', email);
}
}JWT Strategy
import { Logger } from '@tgtone/logger';
const logger = new Logger({ namespace: 'JWTStrategy' });
const jwtExtractor = (req: Request): string | null => {
const token = ExtractJwt.fromAuthHeaderAsBearerToken()(req);
if (token) {
logger.debug('Token found, length:', token.length);
logger.debug('Token expires:', decodedToken.exp);
} else {
logger.debug('No token in request');
}
return token;
};API Module Initialization
import { Logger } from '@tgtone/logger';
const logger = new Logger({ namespace: 'JwtModule' });
JwtModule.registerAsync({
useFactory: (configService: ConfigService) => {
const secret = configService.get('JWT_SECRET');
logger.debug('Initializing with secret length:', secret.length);
logger.debug('Expiration:', configService.get('JWT_EXPIRATION'));
return { secret, signOptions: { expiresIn: '7d' } };
},
});Migration from console.log
Before:
if (process.env.DEBUG === 'true') {
console.log('🔍 Token found:', token);
console.log('📋 Payload:', payload);
}After:
import { debug } from '@tgtone/logger';
debug('🔍 Token found:', token);
debug('📋 Payload:', payload);Production Behavior
In production (NODE_ENV=production and no DEBUG=true):
- ✅
info(),warn(),error()→ shown - ❌
debug()→ suppressed
Development Behavior
In development (NODE_ENV=development or DEBUG=true):
- ✅ All log levels shown
License
MIT
Author
TGT Technology
