Logger
Ajout d'une strategie de log
node example.js[2023-11-21 00:00:00] LOG : hello world
Suppression d'une strategie specifique de log
Suppression de toutes les strategies de log
Création d'une strategie de log
import { appendFileSync } from 'fs';
import { BasaltLogger, LoggerStrategy, LogLevels} from '@basalt-lab/basalt-logger';
class FileLoggerStrategy implements LoggerStrategy {
public log(level: LogLevels, date: Date, object: unknown): void {
const prefixDate: string = `[${date.toISOString().replace(/T/, ' ').replace(/\..+/, '')}]`;
const sanitizedObject: string = typeof object === 'string' ? object : JSON.stringify(object);
const message: string = `${prefixDate} ${level} : ${sanitizedObject}`;
appendFile('./test.log', `${message}\n`, (err): void => {
if (err) throw err;
});
}
}
const logger = new BasaltLogger();
logger.addStrategy('file', new FileLoggerStrategy());
logger.log('Hello World');
const { appendFileSync } = require('fs');
const { BasaltLogger, LogLevels } = require('@basalt-lab/basalt-logger');
class FileLoggerStrategy {
log(level, date, object) {
const prefixDate = `[${date.toISOString().replace(/T/, ' ').replace(/\..+/, '')}]`;
const sanitizedObject = typeof object === 'string' ? object : JSON.stringify(object);
const message = `${prefixDate} ${level} : ${sanitizedObject}`;
appendFile('./test.log', `${message}\n`, (err) => {
if (err) throw err;
});
}
}
const logger = new BasaltLogger();
logger.addStrategy('file', new FileLoggerStrategy());
logger.log('Hello World');
node example.jscat test.log[2023-11-21 00:00:00] LOG : Hello World
Ajout de plusieurs strategies de log
node example.js[2023-11-21 00:00:00] LOG : Hello Worldcat log.txt[2023-11-21 00:00:00] LOG : Hello World
Tout les niveaux de log
import { BasaltLogger, ConsoleLoggerStrategy } from '@basalt-lab/basalt-logger';
const logger = new BasaltLogger();
logger.addStrategy('console', new ConsoleLoggerStrategy());
logger.log('hello world');
logger.info('hello world');
logger.debug('hello world');
logger.warn('hello world');
logger.error('hello world');
const { BasaltLogger, ConsoleLoggerStrategy } = require('@basalt-lab/basalt-logger');
const logger = new BasaltLogger();
logger.addStrategy('console', new ConsoleLoggerStrategy());
logger.log('hello world');
logger.info('hello world');
logger.debug('hello world');
logger.warn('hello world');
logger.error('hello world');
node example.js[2023-11-21 00:00:00] LOG : hello world
[2023-11-21 00:00:00] INFO : hello world
[2023-11-21 00:00:00] DEBUG : hello world
[2023-11-21 00:00:00] WARN : hello world
[2023-11-21 00:00:00] ERROR : hello world
[2023-11-21 00:00:00] INFO : hello world
[2023-11-21 00:00:00] DEBUG : hello world
[2023-11-21 00:00:00] WARN : hello world
[2023-11-21 00:00:00] ERROR : hello world
Log sur une strategie specifique
import { BasaltLogger, ConsoleLoggerStrategy, FileLoggerStrategy} from '@basalt-lab/basalt-logger';
const logger = new BasaltLogger();
logger.addStrategies([
['console', new ConsoleLoggerStrategy()],
['file', new FileLoggerStrategy('log.txt')]
]);
logger.log('console strategy', ['console']);
logger.log('file strategy', ['file']);
logger.log('console and file strategy', ['console', 'file']);
logger.log('all strategies');
const { BasaltLogger, ConsoleLoggerStrategy, FileLoggerStrategy } = require('@basalt-lab/basalt-logger');
const logger = new BasaltLogger();
logger.addStrategies([
['console', new ConsoleLoggerStrategy()],
['file', new FileLoggerStrategy('log.txt')]
]);
logger.log('console strategy', ['console']);
logger.log('file strategy', ['file']);
logger.log('console and file strategy', ['console', 'file']);
logger.log('all strategies');
node example.js[2023-11-21 00:00:00] LOG : console strategy
[2023-11-21 00:00:00] LOG : console and file strategy
[2023-11-21 00:00:00] LOG : all strategiescat log.txt[2023-11-21 00:00:00] LOG : file strategy
[2023-11-21 00:00:00 LOG : console and file strategy
[2023-11-21 00:00:00 LOG : all strategies
[2023-11-21 00:00:00] LOG : console and file strategy
[2023-11-21 00:00:00] LOG : all strategiescat log.txt[2023-11-21 00:00:00] LOG : file strategy
[2023-11-21 00:00:00 LOG : console and file strategy
[2023-11-21 00:00:00 LOG : all strategies