Используется 4 переменных:
process.env.PORT
process.env.NODE_ENV
process.env.TELEGRAM_BOT
process.env.BETTERSTACK_TOKEN
process.env.PORT
используется для подвязки названия сервиса, а уже по названию сервиса подвязываем телеграм чат. Название сервиса подвязывается через PORT по enum OpenPorts, если его там нет, берем noservice
как общее.
process.env.NODE_ENV
используется в TelegramTransport, чтобы использовать его только в production среде.
process.env.BETTERSTACK_TOKEN
используется для Betterstack Transport. Если токена нет, “транспорт” будет проигнорирован.
defaultMeta: {
env: this._env,
service: this._service,
schema: this._schema,
fn: params?.fn || null,
}
import { Winston } from 'newmax-utils';
const Logger = new Winston();
const logger = Logger.createInstance();
logger.error(`Message ${process.env.NODE_ENV}`);
const config = {
schema: 'NAME OF DATABASE SCHEMA' // default: null
}
const Logger = new Winston(config)
const config = {
fn: 'Service or function name',
file: {
level: 'Level of messages', // default: low level into config [winstonConfig.levels](<https://github.com/newmaxcom/core-newmax-utils/blob/main/src/configs/winston.config.ts#L4>) (custom)
disable: 'Boolean flag to add file transport', // default: false
},
console: {
level: 'Level of messages', // default: low level into config [winstonConfig.levels](<https://github.com/newmaxcom/core-newmax-utils/blob/main/src/configs/winston.config.ts#L4>) (custom)
disable: 'Boolean flag to add console transport', // default: false
},
telegram: {
level: 'Level of messages', // default: low level into config [winstonConfig.levels](<https://github.com/newmaxcom/core-newmax-utils/blob/main/src/configs/winston.config.ts#L4>) (custom)
disable: 'Boolean flag to add telegram transport', // default: false
},
}
const Logger = new Winston();
const logger = Logger.createInstance(config);
import { serviceInvoker } from 'newmax-utils';
import Service from '#services/common/Common';
class Controller {
test = async (req, res) => {
await serviceInvoker(req, res, Service.setNomenclature, Service.schema);
};
}
import { Winston, schemaNames } from 'newmax-utils';
class Service {
constructor() {
this.schema = schemaNames.finance;
this.Logger = new Winston({
schema: this.schema,
});
}
createInstance = async () => {
const instance1 = this.Logger.createInstance(); // fn: null (as default)
const instance2 = this.Logger.createInstance({ fn: 'createInstance' });
const instance3 = this.Logger.createInstance({ file: { level: 'info', disable: true } });
};
createProfiler = async () => {
const data = {
level: 'info',
service: 'wber',
schema: 'common',
fn: 'createProfiler',
message: 'Service complited success',
env: process.env.NODE_ENV,
};
const profiler = logger.startTimer();
profiler.done({
message: 'Service complited success',
...data,
});
logger.profile('profiler_name'); // start profiler
logger.profile('profiler_name', { level: 'info', ...data }); // end profiler
};
setNomenclature = async () => {
const logger = this.Logger.createInstance({ fn: 'setNomenclature' });
logger.log({
level: 'info',
message: 'Pass an object and this works',
additional: 'properties',
are: 'passed along',
});
logger.log('info', 'Pass a message and this works', {
additional: 'properties',
are: 'passed along',
});
logger.info('Use a helper method if you want', {
additional: 'properties',
are: 'passed along',
});
logger.log('error', 'Important error: ', new Error('Error passed as meta'));
logger.warn('Maybe important error: ', new Error('Error passed as meta'));
logger.error('', new Error('Error as info')); // message is required else we not see error stack
};
}