cube-ms
Version:
Production-ready microservice framework with health monitoring, validation, error handling, and Docker Swarm support
222 lines (201 loc) • 5.8 kB
JavaScript
import { config } from './config/environment.js';
let CCNLog = null;
let isConfigured = false;
let ccnLogger = null;
/**
* Dynamically import ccn-logging to handle cases where it might not be available
*/
async function loadCCNLogging() {
try {
const ccnLogging = await import('ccn-logging');
return ccnLogging.default || ccnLogging;
} catch (error) {
console.warn('CCN Logging not available:', error.message);
return null;
}
}
/**
* Initialize CCN Logging with configuration
*/
export async function initializeCCNLogging() {
if (!config.logging.ccn.enabled) {
console.info('CCN Logging is disabled');
return false;
}
try {
CCNLog = await loadCCNLogging();
if (!CCNLog) {
console.warn('CCN Logging module not found, falling back to local logging');
return false;
}
// Configure CCN Logging
await CCNLog.configure({
product: config.logging.ccn.product,
serverID: config.logging.ccn.serverID,
appID: config.logging.ccn.appID,
loggingEndpoint: config.logging.ccn.loggingEndpoint,
});
isConfigured = true;
ccnLogger = CCNLog.createLogger('CubeMicroservice');
console.info('CCN Logging initialized successfully', {
product: config.logging.ccn.product,
serverID: config.logging.ccn.serverID,
appID: config.logging.ccn.appID,
endpoint: config.logging.ccn.loggingEndpoint
});
return true;
} catch (error) {
console.error('Failed to initialize CCN Logging:', error);
isConfigured = false;
return false;
}
}
/**
* Create a CCN Logger instance for specific component
*/
export function createCCNLogger(component = 'Application') {
if (!isConfigured || !CCNLog) {
return createFallbackLogger(component);
}
try {
return CCNLog.createLogger(component);
} catch (error) {
console.warn(`Failed to create CCN logger for ${component}:`, error.message);
return createFallbackLogger(component);
}
}
/**
* Fallback logger when CCN Logging is not available
*/
function createFallbackLogger(component) {
return {
info: (action, message, category = '', details = '', logLevel = 'Level3') => {
console.log(`[${component}] INFO: ${action} - ${message}`, { category, details, logLevel });
},
warn: (action, message, category = '', details = '', logLevel = 'Level2') => {
console.warn(`[${component}] WARN: ${action} - ${message}`, { category, details, logLevel });
},
error: (action, message, category = '', details = '', logLevel = 'Level1') => {
console.error(`[${component}] ERROR: ${action} - ${message}`, { category, details, logLevel });
},
debug: (action, message, category = '', details = '', logLevel = 'Level4') => {
console.debug(`[${component}] DEBUG: ${action} - ${message}`, { category, details, logLevel });
}
};
}
/**
* Register central logging for startup
*/
export async function registerCentralLogging() {
if (!isConfigured || !ccnLogger) {
console.info('CCN Logging not configured, using fallback logging');
return;
}
try {
ccnLogger.info(
'registerCentralLogging',
`register cube core to central logging`,
'start up BOX',
JSON.stringify({
service: config.service.name,
version: config.service.version,
environment: config.service.environment,
instance: config.service.instance
}),
config.logging.ccn.logLevel.startup
);
} catch (error) {
console.error('Failed to register central logging:', error);
}
}
/**
* Log successful operations
*/
export function logSuccess(action, message, details = {}) {
if (!isConfigured || !ccnLogger) {
console.log(`SUCCESS: ${action} - ${message}`, details);
return;
}
try {
ccnLogger.info(
action,
message,
'success',
JSON.stringify(details),
config.logging.ccn.logLevel.success
);
} catch (error) {
console.error('Failed to log success:', error);
console.log(`SUCCESS: ${action} - ${message}`, details);
}
}
/**
* Log error operations
*/
export function logError(action, message, error, details = {}) {
if (!isConfigured || !ccnLogger) {
console.error(`ERROR: ${action} - ${message}`, { error: error.message || error, ...details });
return;
}
try {
ccnLogger.error(
action,
message,
'error',
JSON.stringify({
error: error.message || error,
stack: error.stack,
...details
}),
config.logging.ccn.logLevel.error
);
} catch (err) {
console.error('Failed to log error:', err);
console.error(`ERROR: ${action} - ${message}`, { error: error.message || error, ...details });
}
}
/**
* Log request operations
*/
export function logRequest(action, message, requestDetails = {}) {
if (!isConfigured || !ccnLogger) {
console.log(`REQUEST: ${action} - ${message}`, requestDetails);
return;
}
try {
ccnLogger.info(
action,
message,
'request',
JSON.stringify(requestDetails),
config.logging.ccn.logLevel.request
);
} catch (error) {
console.error('Failed to log request:', error);
console.log(`REQUEST: ${action} - ${message}`, requestDetails);
}
}
/**
* Get current CCN logging status
*/
export function getCCNLoggingStatus() {
return {
available: CCNLog !== null,
configured: isConfigured,
config: isConfigured ? {
product: config.logging.ccn.product,
serverID: config.logging.ccn.serverID,
appID: config.logging.ccn.appID,
endpoint: config.logging.ccn.loggingEndpoint
} : null
};
}
export default {
initializeCCNLogging,
createCCNLogger,
registerCentralLogging,
logSuccess,
logError,
logRequest,
getCCNLoggingStatus
};