-
Notifications
You must be signed in to change notification settings - Fork 1
/
log4jslogger.js
44 lines (36 loc) · 1.39 KB
/
log4jslogger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import log4js from 'log4js';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
import getGlobbedFiles from './categoryglobber.js';
/* eslint-disable no-underscore-dangle */
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let configFileName = 'log4jsconfig-development.json';
if (process.env.NODE_ENV) {
const envFileName = `log4jsconfig-${process.env.NODE_ENV}.json`;
if (fs.existsSync(envFileName)) {
configFileName = envFileName;
}
}
const globbedOptions = JSON.parse(fs.readFileSync(`${__dirname}/${configFileName}`));
const unglobbedOptions = getGlobbedFiles(globbedOptions);
// configuration comes from json files
// you can add different configurations for different environments this way.
log4js.configure(unglobbedOptions);
/**
* call like this with es modules: const logger = createModuleLogger(import.meta.url)
* */
function createModuleLogger(fileName) {
let relativeModuleName;
if (import.meta) {
// fun trick https://bobbyhadz.com/blog/javascript-dirname-is-not-defined-in-es-module-scope
const moduleFilePath = fileURLToPath(fileName);
relativeModuleName = path.relative('', moduleFilePath);
} else {
relativeModuleName = path.relative('', __filename);
}
return log4js.getLogger(relativeModuleName);
}
/* eslint-enableno-underscore-dangle */
export { createModuleLogger, log4js };