saget-auth-middleware
Version:
SSO Middleware dengan dukungan localStorage untuk validasi authentifikasi domain malinau.go.id dan semua subdomain pada aplikasi Next.js 14 & 15
130 lines (116 loc) • 3.2 kB
JavaScript
/**
* Enhanced logger with configurable log levels and better formatting
*/
class Logger {
constructor() {
this.logLevel = this.getLogLevel();
this.isDevelopment = process.env.NODE_ENV === 'development';
this.isProduction = process.env.NODE_ENV === 'production';
}
/**
* Get log level from environment or default to 'info'
* @private
* @returns {string} Log level
*/
getLogLevel() {
const level = process.env.SSO_LOG_LEVEL || 'info';
const validLevels = ['debug', 'info', 'warn', 'error', 'silent'];
return validLevels.includes(level.toLowerCase()) ? level.toLowerCase() : 'info';
}
/**
* Check if log level should be output
* @private
* @param {string} level - Log level to check
* @returns {boolean} True if should log
*/
shouldLog(level) {
const levels = { debug: 0, info: 1, warn: 2, error: 3, silent: 4 };
return levels[level] >= levels[this.logLevel];
}
/**
* Format log message with timestamp and level
* @private
* @param {string} level - Log level
* @param {Array} args - Log arguments
* @returns {Array} Formatted arguments
*/
formatMessage(level, args) {
if (!this.isDevelopment && level !== 'error' && level !== 'warn') {
return args;
}
const timestamp = new Date().toISOString();
const prefix = `[${timestamp}] [SSO-${level.toUpperCase()}]`;
return [prefix, ...args];
}
/**
* Debug level logging (development only)
* @param {...any} args - Arguments to log
*/
debug(...args) {
if (this.shouldLog('debug') && this.isDevelopment) {
console.debug(...this.formatMessage('debug', args));
}
}
/**
* Info level logging
* @param {...any} args - Arguments to log
*/
info(...args) {
if (this.shouldLog('info') && this.isDevelopment) {
console.info(...this.formatMessage('info', args));
}
}
/**
* Warning level logging (always shown)
* @param {...any} args - Arguments to log
*/
warn(...args) {
if (this.shouldLog('warn')) {
console.warn(...this.formatMessage('warn', args));
}
}
/**
* Error level logging (always shown)
* @param {...any} args - Arguments to log
*/
error(...args) {
if (this.shouldLog('error')) {
console.error(...this.formatMessage('error', args));
}
}
/**
* Log with custom level
* @param {string} level - Log level
* @param {...any} args - Arguments to log
*/
log(level = 'info', ...args) {
if (typeof level !== 'string') {
// Backward compatibility: if first arg is not a string, treat as info
this.info(level, ...args);
return;
}
switch (level.toLowerCase()) {
case 'debug':
this.debug(...args);
break;
case 'info':
this.info(...args);
break;
case 'warn':
this.warn(...args);
break;
case 'error':
this.error(...args);
break;
default:
this.info(...args);
}
}
}
// Create singleton instance
const logger = new Logger();
// Make logger available globally for backward compatibility
if (typeof global !== 'undefined') {
global.logger = logger;
}
export { logger };