Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import KeyboardProviderDelegater from './providers/KeyboardProviderDelegater';
import AckChallengeProviderDelegater from './providers/AckChallengeDelegater';
import UserInterestDelegater from './providers/UserInterestDelegater';
const logger = require('./utils/Logger')('App.js');
const baseLogger = require('./utils/Logger');
import FireboltTransportInvoker from './FireboltTransportInvoker';
import { handleAsyncFunction } from './utils/Utils';
import { withAnnouncer } from '@lightningjs/ui-components';
Expand Down Expand Up @@ -83,6 +84,7 @@ export default class App extends Base {

async _init() {
Settings.setLogLevel('DEBUG');
baseLogger.promoteTempLog('Adi-123')
eventEmitter.on('showToast', (message, state, tagName, color) => {
this.showToast(message, state, tagName, color);
});
Expand Down
123 changes: 117 additions & 6 deletions src/utils/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,134 @@
* SPDX-License-Identifier: Apache-2.0
*/

// const winston = require('winston');
// const { CONSTANTS } = require('../constant');

// const logConfiguration = {
// transports: [new winston.transports.Console({ level: CONSTANTS.LOGGER_LEVEL })],
// format: winston.format.combine(
// winston.format.timestamp({
// format: 'MMM-DD-YYYY HH:mm:ss',
// }),
// winston.format.printf((options) => {
// const args = options[Symbol.for('splat')];
// return `[${options.timestamp}][${options.level}][${options.moduleName}][${args}][${options.message}]`;
// })
// ),
// };
// const logger = winston.createLogger(logConfiguration);

// module.exports = function (name) {
// return logger.child({ moduleName: name });
// };

/**
* Copyright 2024 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
const winston = require('winston');
const fs = require('fs');
const path = require('path');
const { CONSTANTS } = require('../constant');

const LOG_PATH_PREFIX = './opt/logs/fca';
const TEMP_LOG_PATH = path.join(LOG_PATH_PREFIX, 'logstemp.log');

const transports = [
new winston.transports.Console({
level: 'debug',
stderrLevels: ['error'],
consoleWarnLevels: [],
}),
];

let fileTransport = null;

// Enable file logging
try {
fs.mkdirSync(LOG_PATH_PREFIX, { recursive: true });
fs.writeFileSync(TEMP_LOG_PATH, '', { flag: 'a' }); // Touch temp file
fileTransport = new winston.transports.File({ filename: TEMP_LOG_PATH, level: CONSTANTS.LOGGER_LEVEL });
transports.push(fileTransport);
} catch (err) {
console.warn('[A] File system not writable, using console-only logs.');
}

const logConfiguration = {
transports: [new winston.transports.Console({ level: CONSTANTS.LOGGER_LEVEL })],
transports,
format: winston.format.combine(
winston.format.timestamp({
format: 'MMM-DD-YYYY HH:mm:ss',
}),
winston.format.timestamp({ format: 'MMM-DD-YYYY HH:mm:ss' }),
winston.format.printf((options) => {
const args = options[Symbol.for('splat')];
return `[${options.timestamp}][${options.level}][${options.moduleName}][${args}][${options.message}]`;
const argsString = args ? ` [${args}]` : '';
const moduleNameString = options.moduleName ? ` [${options.moduleName}]` : '';
return `[${options.timestamp}] [${options.level}]${moduleNameString}${argsString}: [${JSON.stringify(options.message) || ''}]`;
})
),
};

const logger = winston.createLogger(logConfiguration);

module.exports = function (name) {
// Exported logger function
const getLogger = function (name) {
return logger.child({ moduleName: name });
};

// Add updateLoggerLevel to export
getLogger.updateLoggerLevel = function (level) {
for (const t of logger.transports) {
t.level = level;
}
console.log(`[A] Log level updated to ${level}`);
};

// Add promoteTempLog to export
getLogger.promoteTempLog = function (jobId) {
console.log("[A] ~ promoteTempLog:" )
logger.transports.forEach((t, index) => {
if (t instanceof winston.transports.File) {
console.log(`[A] Transport[${index}] is a File transport:`, t.filename);
} else {
console.log(`[A] Transport[${index}] is NOT a File transport`);
}
});
const tempFile = logger.transports.find(
t => t instanceof winston.transports.File && t.filename === 'logstemp.log'
);

if (!tempFile) {
console.warn('[A] No temp file transport found.');
return;
}

logger.remove(tempFile);

const finalDir = path.join(LOG_PATH_PREFIX, jobId);
const finalPath = path.join(finalDir, 'fca.log');

try {
fs.mkdirSync(finalDir, { recursive: true });
fs.renameSync(TEMP_LOG_PATH, finalPath);
const newFileTransport = new winston.transports.File({ filename: finalPath, level: 'debug' });
logger.add(newFileTransport);
console.log(`[A] Promoted logs to ${finalPath}`);
} catch (err) {
console.warn(`[A] Failed to promote temp log file: ${err.message}`);
}
};

module.exports = getLogger;

Loading