@unchainedshop/logger 3.0.0-alpha7 → 3.0.0-rc2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,3 @@
1
- import { format, transports } from 'winston';
2
- import TransportStream from 'winston-transport';
3
- export { transports, format };
4
- export declare const createLogger: (moduleName: string, moreTransports?: Array<TransportStream>) => import("winston").Logger;
1
+ import { default as log } from 'loglevel';
2
+ export declare const createLogger: (moduleName: string) => log.Logger;
5
3
  //# sourceMappingURL=createLogger.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createLogger.d.ts","sourceRoot":"","sources":["../src/createLogger.ts"],"names":[],"mappings":"AACA,OAAO,EAAuC,MAAM,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAClF,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAmDhD,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC;AAE9B,eAAO,MAAM,YAAY,eAAgB,MAAM,mBAAkB,KAAK,CAAC,eAAe,CAAC,6BAkBtF,CAAC"}
1
+ {"version":3,"file":"createLogger.d.ts","sourceRoot":"","sources":["../src/createLogger.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,IAAI,GAAG,EAAE,MAAM,UAAU,CAAC;AAsE1C,eAAO,MAAM,YAAY,eAAgB,MAAM,eAa9C,CAAC"}
@@ -1,10 +1,9 @@
1
- import { createRequire } from 'module';
2
- import { createLogger as createWinstonLogger, format, transports } from 'winston';
1
+ import { stringify } from 'safe-stable-stringify';
2
+ import { default as log } from 'loglevel';
3
3
  import { LogLevel } from './logger.types.js';
4
- const require = createRequire(import.meta.url);
5
- const { stringify } = require('safe-stable-stringify');
4
+ import { default as prefix } from 'loglevel-plugin-prefix';
5
+ import chalk from 'chalk';
6
6
  const { DEBUG = '', LOG_LEVEL = LogLevel.Info, UNCHAINED_LOG_FORMAT = 'unchained' } = process.env;
7
- const { combine, colorize, json } = format;
8
7
  const debugStringContainsModule = (debugString, moduleName) => {
9
8
  if (!debugString)
10
9
  return false;
@@ -24,38 +23,52 @@ const debugStringContainsModule = (debugString, moduleName) => {
24
23
  }, undefined);
25
24
  return loggingMatched || false;
26
25
  };
27
- const myFormat = format.printf(({ level, message, label, timestamp, stack, ...rest }) => {
28
- const otherPropsString = stringify(rest);
29
- return [
30
- `${timestamp} [${label}] ${level}:`,
31
- `${message}`,
32
- `${otherPropsString}`,
33
- stack ? `\n${stack}` : null,
34
- ]
35
- .filter(Boolean)
36
- .join(' ');
37
- });
38
- const UnchainedLogFormats = {
39
- unchained: combine(colorize(), myFormat),
40
- json: json(),
26
+ const colors = {
27
+ TRACE: chalk.magenta,
28
+ DEBUG: chalk.cyan,
29
+ INFO: chalk.blue,
30
+ WARN: chalk.yellow,
31
+ ERROR: chalk.red,
41
32
  };
42
- if (!UnchainedLogFormats[UNCHAINED_LOG_FORMAT.toLowerCase()]) {
43
- throw new Error(`UNCHAINED_LOG_FORMAT is invalid, use one of ${Object.keys(UnchainedLogFormats).join(',')}`);
33
+ const invertedLevels = Object.fromEntries(Object.entries(log.levels).map(([key, value]) => [value, key]));
34
+ const SUPPORTED_LOG_FORMATS = ['json', 'unchained'];
35
+ if (!SUPPORTED_LOG_FORMATS.includes(UNCHAINED_LOG_FORMAT.toLowerCase())) {
36
+ throw new Error(`UNCHAINED_LOG_FORMAT is invalid, use one of ${SUPPORTED_LOG_FORMATS.join(',')}`);
44
37
  }
45
- export { transports, format };
46
- export const createLogger = (moduleName, moreTransports = []) => {
47
- const loggingMatched = debugStringContainsModule(DEBUG, moduleName);
48
- return createWinstonLogger({
49
- format: format.combine(format.errors({ stack: process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test' }), format.timestamp(), format.label({ label: moduleName })),
50
- transports: [
51
- new transports.Console({
52
- format: UnchainedLogFormats[UNCHAINED_LOG_FORMAT],
53
- stderrLevels: [LogLevel.Error],
54
- consoleWarnLevels: [LogLevel.Warning],
55
- level: loggingMatched ? LogLevel.Debug : LOG_LEVEL,
56
- }),
57
- ...moreTransports,
58
- ],
38
+ if (UNCHAINED_LOG_FORMAT.toLowerCase() === 'unchained') {
39
+ prefix.reg(log);
40
+ prefix.apply(log, {
41
+ format: (level, name, timestamp) => `${chalk.gray(`${timestamp}`)} [${chalk.green(`${name}] ${colors[level.toUpperCase()](level)}:`)}`,
59
42
  });
43
+ }
44
+ else if (UNCHAINED_LOG_FORMAT.toLowerCase() === 'json') {
45
+ const originalFactory = log.methodFactory;
46
+ log.methodFactory = function (methodName, logLevel, loggerName) {
47
+ const rawMethod = originalFactory(methodName, logLevel, loggerName);
48
+ const level = invertedLevels[logLevel];
49
+ const name = loggerName || 'unchained';
50
+ return function (message, meta) {
51
+ rawMethod(stringify({
52
+ timestamp: new Date(),
53
+ level,
54
+ name,
55
+ message,
56
+ ...meta,
57
+ }));
58
+ };
59
+ };
60
+ log.rebuild();
61
+ }
62
+ export const createLogger = (moduleName) => {
63
+ const loggingMatched = debugStringContainsModule(DEBUG, moduleName);
64
+ const logger = log.getLogger(moduleName);
65
+ const logLevelMap = {
66
+ [LogLevel.Debug]: log.levels.DEBUG,
67
+ [LogLevel.Info]: log.levels.INFO,
68
+ [LogLevel.Warning]: log.levels.WARN,
69
+ [LogLevel.Error]: log.levels.ERROR,
70
+ };
71
+ logger.setDefaultLevel(loggingMatched ? log.levels.DEBUG : logLevelMap[LOG_LEVEL.toLowerCase()]);
72
+ return logger;
60
73
  };
61
74
  //# sourceMappingURL=createLogger.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"createLogger.js","sourceRoot":"","sources":["../src/createLogger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AACvC,OAAO,EAAE,YAAY,IAAI,mBAAmB,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAElF,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,MAAM,OAAO,GAAG,aAA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AM,MAAM,GAAG,GAAG,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;IAEzC,MAAM,WAAW,GAAG;QAClB,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,KAAK;QAClC,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,IAAI;QAChC,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,IAAI;QACnC,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,KAAK;KACnC,CAAC;IAEF,MAAM,CAAC,eAAe,CAAC,cAAc,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;IACjG,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}
package/lib/log.d.ts CHANGED
@@ -1,4 +1,2 @@
1
- import winston from 'winston';
2
- import { LogOptions } from './logger.types.js';
3
- export declare const log: (message: string | Error, options?: LogOptions) => winston.Logger;
1
+ export declare const log: (...msg: any[]) => void;
4
2
  //# sourceMappingURL=log.d.ts.map
package/lib/log.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAY,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAKzD,eAAO,MAAM,GAAG,YAAa,MAAM,GAAG,KAAK,YAAY,UAAU,KAAG,OAAO,CAAC,MAM3E,CAAC"}
1
+ {"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,GAAG,yBAAc,CAAC"}
package/lib/log.js CHANGED
@@ -1,11 +1,4 @@
1
- import { LogLevel } from './logger.types.js';
2
1
  import { createLogger } from './createLogger.js';
3
2
  const logger = createLogger('unchained');
4
- export const log = (message, options) => {
5
- if (!options?.level) {
6
- return logger.info(message, options);
7
- }
8
- const { level = LogLevel.Info, ...meta } = options || {};
9
- return logger.log(level, message, meta);
10
- };
3
+ export const log = logger.info;
11
4
  //# sourceMappingURL=log.js.map
package/lib/log.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"log.js","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAc,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,MAAM,GAAG,YAAY,CAAC,WAAW,CAAC,CAAC;AAEzC,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC,OAAuB,EAAE,OAAoB,EAAkB,EAAE;IACnF,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC;QACpB,OAAO,MAAM,CAAC,IAAI,CAAC,OAAc,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC;IACD,MAAM,EAAE,KAAK,GAAG,QAAQ,CAAC,IAAI,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;IACzD,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,OAAc,EAAE,IAAI,CAAC,CAAC;AACjD,CAAC,CAAC"}
1
+ {"version":3,"file":"log.js","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,MAAM,GAAG,YAAY,CAAC,WAAW,CAAC,CAAC;AAEzC,MAAM,CAAC,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC"}
@@ -1,5 +1,4 @@
1
- import { createLogger, format, transports } from './createLogger.js';
2
- import { LogLevel } from './logger.types.js';
3
- import { log } from './log.js';
4
- export { log, createLogger, format, transports, LogLevel };
1
+ export * from './createLogger.js';
2
+ export * from './logger.types.js';
3
+ export * from './log.js';
5
4
  //# sourceMappingURL=logger-index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"logger-index.d.ts","sourceRoot":"","sources":["../src/logger-index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,OAAO,EAAE,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC"}
1
+ {"version":3,"file":"logger-index.d.ts","sourceRoot":"","sources":["../src/logger-index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,UAAU,CAAC"}
@@ -1,5 +1,4 @@
1
- import { createLogger, format, transports } from './createLogger.js';
2
- import { LogLevel } from './logger.types.js';
3
- import { log } from './log.js';
4
- export { log, createLogger, format, transports, LogLevel };
1
+ export * from './createLogger.js';
2
+ export * from './logger.types.js';
3
+ export * from './log.js';
5
4
  //# sourceMappingURL=logger-index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"logger-index.js","sourceRoot":"","sources":["../src/logger-index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,OAAO,EAAE,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC"}
1
+ {"version":3,"file":"logger-index.js","sourceRoot":"","sources":["../src/logger-index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,UAAU,CAAC"}
@@ -1,5 +1,3 @@
1
- import { LoggerOptions } from 'winston';
2
- export { format, transports } from 'winston';
3
1
  export declare enum LogLevel {
4
2
  Verbose = "verbose",
5
3
  Info = "info",
@@ -7,8 +5,4 @@ export declare enum LogLevel {
7
5
  Error = "error",
8
6
  Warning = "warn"
9
7
  }
10
- export interface LogOptions extends LoggerOptions {
11
- level?: LogLevel;
12
- [x: string]: any;
13
- }
14
8
  //# sourceMappingURL=logger.types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"logger.types.d.ts","sourceRoot":"","sources":["../src/logger.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE7C,oBAAY,QAAQ;IAClB,OAAO,YAAY;IACnB,IAAI,SAAS;IACb,KAAK,UAAU;IACf,KAAK,UAAU;IACf,OAAO,SAAS;CACjB;AAED,MAAM,WAAW,UAAW,SAAQ,aAAa;IAC/C,KAAK,CAAC,EAAE,QAAQ,CAAC;IACjB,CAAC,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;CAClB"}
1
+ {"version":3,"file":"logger.types.d.ts","sourceRoot":"","sources":["../src/logger.types.ts"],"names":[],"mappings":"AAAA,oBAAY,QAAQ;IAClB,OAAO,YAAY;IACnB,IAAI,SAAS;IACb,KAAK,UAAU;IACf,KAAK,UAAU;IACf,OAAO,SAAS;CACjB"}
@@ -1,4 +1,3 @@
1
- export { format, transports } from 'winston';
2
1
  export var LogLevel;
3
2
  (function (LogLevel) {
4
3
  LogLevel["Verbose"] = "verbose";
@@ -1 +1 @@
1
- {"version":3,"file":"logger.types.js","sourceRoot":"","sources":["../src/logger.types.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE7C,MAAM,CAAN,IAAY,QAMX;AAND,WAAY,QAAQ;IAClB,+BAAmB,CAAA;IACnB,yBAAa,CAAA;IACb,2BAAe,CAAA;IACf,2BAAe,CAAA;IACf,4BAAgB,CAAA;AAClB,CAAC,EANW,QAAQ,KAAR,QAAQ,QAMnB"}
1
+ {"version":3,"file":"logger.types.js","sourceRoot":"","sources":["../src/logger.types.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,QAMX;AAND,WAAY,QAAQ;IAClB,+BAAmB,CAAA;IACnB,yBAAa,CAAA;IACb,2BAAe,CAAA;IACf,2BAAe,CAAA;IACf,4BAAgB,CAAA;AAClB,CAAC,EANW,QAAQ,KAAR,QAAQ,QAMnB"}
package/package.json CHANGED
@@ -1,16 +1,13 @@
1
1
  {
2
2
  "name": "@unchainedshop/logger",
3
- "version": "3.0.0-alpha7",
3
+ "version": "3.0.0-rc2",
4
4
  "main": "lib/logger-index.js",
5
- "exports": {
6
- ".": "./lib/logger-index.js",
7
- "./*": "./lib/*"
8
- },
9
5
  "types": "lib/logger-index.d.ts",
10
6
  "type": "module",
11
7
  "scripts": {
12
- "clean": "rm -rf lib",
13
- "prepublishOnly": "npm run clean && tsc",
8
+ "clean": "tsc -b --clean",
9
+ "build": "tsc -b",
10
+ "prepublishOnly": "npm run clean && npm run build",
14
11
  "watch": "tsc -w",
15
12
  "test": "NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --forceExit",
16
13
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --detectOpenHandles --forceExit"
@@ -31,15 +28,16 @@
31
28
  },
32
29
  "homepage": "https://github.com/unchainedshop/unchained#readme",
33
30
  "dependencies": {
34
- "safe-stable-stringify": "^2.5.0",
35
- "winston": "^3.15.0",
36
- "winston-transport": "^4.8.0"
31
+ "chalk": "^5.3.0",
32
+ "loglevel": "^1.9.2",
33
+ "loglevel-plugin-prefix": "^0.8.4",
34
+ "safe-stable-stringify": "^2.5.0"
37
35
  },
38
36
  "devDependencies": {
39
37
  "@types/jest": "^29.5.14",
40
- "@types/node": "^22.8.1",
38
+ "@types/node": "^22.10.2",
41
39
  "jest": "^29.7.0",
42
40
  "ts-jest": "^29.2.5",
43
- "typescript": "^5.6.3"
41
+ "typescript": "^5.7.2"
44
42
  }
45
43
  }
@@ -1,15 +1,11 @@
1
- import { createRequire } from 'module';
2
- import { createLogger as createWinstonLogger, format, transports } from 'winston';
3
- import TransportStream from 'winston-transport';
1
+ import { stringify } from 'safe-stable-stringify';
2
+ import { default as log } from 'loglevel';
4
3
  import { LogLevel } from './logger.types.js';
5
-
6
- const require = createRequire(import.meta.url);
7
- const { stringify } = require('safe-stable-stringify');
4
+ import { default as prefix } from 'loglevel-plugin-prefix';
5
+ import chalk from 'chalk';
8
6
 
9
7
  const { DEBUG = '', LOG_LEVEL = LogLevel.Info, UNCHAINED_LOG_FORMAT = 'unchained' } = process.env;
10
8
 
11
- const { combine, colorize, json } = format;
12
-
13
9
  const debugStringContainsModule = (debugString: string, moduleName: string) => {
14
10
  if (!debugString) return false;
15
11
  const loggingMatched = debugString.split(',').reduce((accumulator: any, name: string) => {
@@ -28,47 +24,62 @@ const debugStringContainsModule = (debugString: string, moduleName: string) => {
28
24
  return loggingMatched || false;
29
25
  };
30
26
 
31
- const myFormat = format.printf(({ level, message, label, timestamp, stack, ...rest }) => {
32
- const otherPropsString: string = stringify(rest);
33
- return [
34
- `${timestamp} [${label}] ${level}:`,
35
- `${message}`,
36
- `${otherPropsString}`,
37
- stack ? `\n${stack}` : null,
38
- ]
39
- .filter(Boolean)
40
- .join(' ');
41
- });
42
-
43
- const UnchainedLogFormats = {
44
- unchained: combine(colorize(), myFormat),
45
- json: json(),
27
+ const colors = {
28
+ TRACE: chalk.magenta,
29
+ DEBUG: chalk.cyan,
30
+ INFO: chalk.blue,
31
+ WARN: chalk.yellow,
32
+ ERROR: chalk.red,
46
33
  };
47
34
 
48
- if (!UnchainedLogFormats[UNCHAINED_LOG_FORMAT.toLowerCase()]) {
49
- throw new Error(
50
- `UNCHAINED_LOG_FORMAT is invalid, use one of ${Object.keys(UnchainedLogFormats).join(',')}`,
51
- );
35
+ const invertedLevels = Object.fromEntries(
36
+ Object.entries(log.levels).map(([key, value]) => [value, key]),
37
+ );
38
+
39
+ const SUPPORTED_LOG_FORMATS = ['json', 'unchained'];
40
+ if (!SUPPORTED_LOG_FORMATS.includes(UNCHAINED_LOG_FORMAT.toLowerCase())) {
41
+ throw new Error(`UNCHAINED_LOG_FORMAT is invalid, use one of ${SUPPORTED_LOG_FORMATS.join(',')}`);
52
42
  }
53
43
 
54
- export { transports, format };
44
+ if (UNCHAINED_LOG_FORMAT.toLowerCase() === 'unchained') {
45
+ prefix.reg(log);
46
+ prefix.apply(log, {
47
+ format: (level, name, timestamp) =>
48
+ `${chalk.gray(`${timestamp}`)} [${chalk.green(`${name}] ${colors[level.toUpperCase()](level)}:`)}`,
49
+ });
50
+ } else if (UNCHAINED_LOG_FORMAT.toLowerCase() === 'json') {
51
+ const originalFactory = log.methodFactory;
52
+ log.methodFactory = function (methodName, logLevel, loggerName) {
53
+ const rawMethod = originalFactory(methodName, logLevel, loggerName);
54
+ const level = invertedLevels[logLevel];
55
+ const name = loggerName || 'unchained';
56
+
57
+ return function (message, meta) {
58
+ rawMethod(
59
+ stringify({
60
+ timestamp: new Date(),
61
+ level,
62
+ name,
63
+ message,
64
+ ...meta,
65
+ }),
66
+ );
67
+ };
68
+ };
69
+ log.rebuild();
70
+ }
55
71
 
56
- export const createLogger = (moduleName: string, moreTransports: Array<TransportStream> = []) => {
72
+ export const createLogger = (moduleName: string) => {
57
73
  const loggingMatched = debugStringContainsModule(DEBUG, moduleName);
58
- return createWinstonLogger({
59
- format: format.combine(
60
- format.errors({ stack: process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test' }),
61
- format.timestamp(),
62
- format.label({ label: moduleName }),
63
- ),
64
- transports: [
65
- new transports.Console({
66
- format: UnchainedLogFormats[UNCHAINED_LOG_FORMAT],
67
- stderrLevels: [LogLevel.Error],
68
- consoleWarnLevels: [LogLevel.Warning],
69
- level: loggingMatched ? LogLevel.Debug : LOG_LEVEL,
70
- }),
71
- ...moreTransports,
72
- ],
73
- });
74
+ const logger = log.getLogger(moduleName);
75
+
76
+ const logLevelMap = {
77
+ [LogLevel.Debug]: log.levels.DEBUG,
78
+ [LogLevel.Info]: log.levels.INFO,
79
+ [LogLevel.Warning]: log.levels.WARN,
80
+ [LogLevel.Error]: log.levels.ERROR,
81
+ };
82
+
83
+ logger.setDefaultLevel(loggingMatched ? log.levels.DEBUG : logLevelMap[LOG_LEVEL.toLowerCase()]);
84
+ return logger;
74
85
  };
package/src/log.ts CHANGED
@@ -1,13 +1,5 @@
1
- import winston from 'winston';
2
- import { LogLevel, LogOptions } from './logger.types.js';
3
1
  import { createLogger } from './createLogger.js';
4
2
 
5
3
  const logger = createLogger('unchained');
6
4
 
7
- export const log = (message: string | Error, options?: LogOptions): winston.Logger => {
8
- if (!options?.level) {
9
- return logger.info(message as any, options);
10
- }
11
- const { level = LogLevel.Info, ...meta } = options || {};
12
- return logger.log(level, message as any, meta);
13
- };
5
+ export const log = logger.info;
@@ -1,5 +1,3 @@
1
- import { createLogger, format, transports } from './createLogger.js';
2
- import { LogLevel } from './logger.types.js';
3
- import { log } from './log.js';
4
-
5
- export { log, createLogger, format, transports, LogLevel };
1
+ export * from './createLogger.js';
2
+ export * from './logger.types.js';
3
+ export * from './log.js';
@@ -1,7 +1,3 @@
1
- import { LoggerOptions } from 'winston';
2
-
3
- export { format, transports } from 'winston';
4
-
5
1
  export enum LogLevel {
6
2
  Verbose = 'verbose',
7
3
  Info = 'info',
@@ -9,8 +5,3 @@ export enum LogLevel {
9
5
  Error = 'error',
10
6
  Warning = 'warn',
11
7
  }
12
-
13
- export interface LogOptions extends LoggerOptions {
14
- level?: LogLevel;
15
- [x: string]: any;
16
- }
package/tsconfig.json CHANGED
@@ -1,11 +1,9 @@
1
1
  {
2
2
  "extends": "../shared/base.tsconfig.json",
3
3
  "compilerOptions": {
4
- "rootDir": "src",
5
- "outDir": "lib",
4
+ "declarationDir": "./lib",
5
+ "rootDir": "./src",
6
+ "outDir": "./lib"
6
7
  },
7
- "exclude": ["**/*.test.ts", "**/*.test.js", "tests"],
8
- "include": [
9
- "./src"
10
- ]
11
- }
8
+ "exclude": ["**/*.test.ts", "**/*.test.js", "tests", "lib"]
9
+ }