From 9ba9fdf54ee44080a4b81682737c7fab6756c6ff Mon Sep 17 00:00:00 2001
From: Piotr Smialkowski
Date: Tue, 7 Mar 2023 21:05:22 +0100
Subject: [PATCH] Fix karma tests
---
.../logging/{karma.conf.js => karma.conf.cjs} | 2 +-
packages/logging/package.json | 50 +++++++++----------
packages/logging/src/defaults.ts | 2 +-
packages/logging/src/formaters.ts | 8 +--
packages/logging/src/handlers.ts | 2 +-
packages/logging/src/logger.ts | 41 ++++++++-------
packages/logging/src/types.ts | 19 +++----
packages/logging/src/utils.ts | 6 ++-
packages/logging/tests/format.test.ts | 7 ++-
packages/logging/tsconfig.json | 6 ++-
10 files changed, 80 insertions(+), 63 deletions(-)
rename packages/logging/{karma.conf.js => karma.conf.cjs} (95%)
diff --git a/packages/logging/karma.conf.js b/packages/logging/karma.conf.cjs
similarity index 95%
rename from packages/logging/karma.conf.js
rename to packages/logging/karma.conf.cjs
index d5b6b01..0f2dde2 100644
--- a/packages/logging/karma.conf.js
+++ b/packages/logging/karma.conf.cjs
@@ -27,6 +27,6 @@ module.exports = function(config) {
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_WARN,
autoWatch: true,
- browsers: ['Chrome', 'Firefox']
+ browsers: ['Chrome'],
});
};
diff --git a/packages/logging/package.json b/packages/logging/package.json
index fa3325e..ce2468f 100644
--- a/packages/logging/package.json
+++ b/packages/logging/package.json
@@ -1,6 +1,6 @@
{
"name": "@stool/logging",
- "version": "3.1.0",
+ "version": "3.2.0",
"description": "Logger",
"license": "MIT",
"homepage": "https://github.com/smialy/stool#readme",
@@ -9,46 +9,44 @@
"url": "git+https://github.com/smialy/stool.git"
},
"source": "src/index.ts",
- "main": "dist/index.js",
+ "main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/types/index.d.ts",
+ "exports": {
+ ".": {
+ "browser": "./dist/index.mjs",
+ "import": "./dist/index.mjs",
+ "require": "./dist/index.cjs"
+ }
+ },
"scripts": {
"build": "micropack",
"dev": "micropack --watch",
"test": "mocha --require ts-node/register tests/*.ts",
- "karma": "karma start --single-run",
+ "karma": "karma start karma.conf.cjs --single-run",
"lint": "eslint src/*",
"format": "prettier src/*.ts tests --write",
"prepare": "npm run build",
"version": "npm run build"
},
- "exports": {
- ".": {
- "require": "./dist/index.js",
- "import": "./dist/index.mjs"
- }
- },
"devDependencies": {
- "@stool/micropack": "0.3.0",
- "@types/chai": "4.2.21",
- "@types/mocha": "8.2.3",
- "@typescript-eslint/eslint-plugin": "^4.28.2",
- "chai": "4.3.4",
- "karma": "6.3.4",
- "karma-chrome-launcher": "3.1.0",
- "karma-firefox-launcher": "2.1.1",
+ "@stool/micropack": "0.4.0",
+ "@types/chai": "4.3.4",
+ "@types/mocha": "10.0.1",
+ "@typescript-eslint/eslint-plugin": "5.54.1",
+ "chai": "^4.3.7",
+ "eslint": "8.35.0",
+ "karma": "6.4.1",
+ "karma-chrome-launcher": "3.1.1",
"karma-mocha": "2.0.1",
- "karma-typescript": "5.5.1",
- "mocha": "9.0.2",
- "prettier": "^2.3.2",
- "ts-node": "10.0.0",
- "typescript": "4.3.5"
+ "karma-typescript": "5.5.3",
+ "mocha": "10.2.0",
+ "prettier": "^2.8.4",
+ "ts-node": "10.9.1",
+ "typescript": "4.9.5"
},
"files": [
"dist/**/*.*",
"README.md"
- ],
- "dependencies": {
- "eslint": "^7.30.0"
- }
+ ]
}
diff --git a/packages/logging/src/defaults.ts b/packages/logging/src/defaults.ts
index 2fbeaf1..0bfbfd9 100644
--- a/packages/logging/src/defaults.ts
+++ b/packages/logging/src/defaults.ts
@@ -1,7 +1,7 @@
import { ILogger } from './types';
import { LoggerFactory } from './logger';
-const NAME = 'global-logger-factory';
+const NAME = Symbol('global.stool.logger.factory');
if (!globalThis[NAME]) {
diff --git a/packages/logging/src/formaters.ts b/packages/logging/src/formaters.ts
index 7e8f054..ed09798 100644
--- a/packages/logging/src/formaters.ts
+++ b/packages/logging/src/formaters.ts
@@ -21,7 +21,7 @@ export const DEFAULT_FORMATERS: TFormaters = {
return [
created.getUTCFullYear(),
'-',
- pad10(created.getUTCMonth()+1),
+ pad10(created.getUTCMonth() + 1),
'-',
pad10(created.getUTCDate()),
'T',
@@ -41,8 +41,8 @@ const DEFAULT_TEMPLATE = '{created}|{levelName}|{name}|{msg}{exception}';
export class SimpleFormater implements IFormater {
static FULL = DEFAULT_TEMPLATE;
- static BASIC = "{levelName}|{name}|{msg}{exception}";
- static MINIMAL = "{msg}{exception}";
+ static BASIC = "{levelName}|{name}|{msg} {extra} {exception}";
+ static MINIMAL = "{msg}{extra}{exception}";
constructor(
private template: string = DEFAULT_TEMPLATE,
@@ -52,7 +52,7 @@ export class SimpleFormater implements IFormater {
format(record: IRecord): string {
return this.template.replace(/\{(.+?)\}/g, (_, name) => {
return this.getFormatted(record, name);
- });
+ }).trim();
}
getFormatted(record: IRecord, name: string): string {
const value = record[name];
diff --git a/packages/logging/src/handlers.ts b/packages/logging/src/handlers.ts
index 5faa3d6..f6cd9f2 100644
--- a/packages/logging/src/handlers.ts
+++ b/packages/logging/src/handlers.ts
@@ -26,8 +26,8 @@ export abstract class BaseHandler extends Filterer implements IHandler {
}
return false;
}
+ // eslint-disable-line
public emit(record: IRecord) {
- // eslint-disable-line
throw new Error('Not implemented');
}
public flush() {
diff --git a/packages/logging/src/logger.ts b/packages/logging/src/logger.ts
index afdec4d..ab49bd9 100644
--- a/packages/logging/src/logger.ts
+++ b/packages/logging/src/logger.ts
@@ -1,7 +1,9 @@
import { LEVEL_NAMES, Levels, ROOT_LOGGER_NAME } from './consts';
import { Filterer } from './filter';
import { IHandler, ILogger, ILoggerFactory, IRecord, LevelType } from './types';
-import { checkLevel } from './utils';
+import { checkLevel, isException } from './utils';
+
+type TExtra = Record;
export class Logger extends Filterer implements ILogger {
public manager?: any;
@@ -50,28 +52,28 @@ export class Logger extends Filterer implements ILogger {
return Array.from(this._handlers);
}
- public fatal(msg: string, exception?: any) {
- this.log(Levels.FATAL, msg, exception);
+ public fatal(msg: string, exception?: any, extra?: TExtra) {
+ this.log(Levels.FATAL, msg, exception, extra);
}
- public critical(msg: string, exception?: any) {
- this.log(Levels.CRITICAL, msg, exception);
+ public critical(msg: string, exception?: any, extra?: TExtra) {
+ this.log(Levels.CRITICAL, msg, exception, extra);
}
- public error(msg: string, exception?: any) {
- this.log(Levels.ERROR, msg, exception);
+ public error(msg: string, exception?: any, extra?: TExtra) {
+ this.log(Levels.ERROR, msg, exception, extra);
}
- public warn(msg: string, exception?: any) {
- this.log(Levels.WARN, msg, exception);
+ public warn(msg: string, exception?: any, extra?: TExtra) {
+ this.log(Levels.WARN, msg, exception, extra);
}
- public warning(msg: string, exception?: any) {
- this.log(Levels.WARN, msg, exception);
+ public warning(msg: string, exception?: any, extra?: TExtra) {
+ this.log(Levels.WARN, msg, exception, extra);
}
- public info(msg: string) {
- this.log(Levels.INFO, msg);
+ public info(msg: string, extra?: TExtra) {
+ this.log(Levels.INFO, msg, extra);
}
public debug(msg: string) {
@@ -82,7 +84,11 @@ export class Logger extends Filterer implements ILogger {
this.log(Levels.ERROR, exception.message, exception);
}
- public log(level: number, msg: string, exception?: any) {
+ public log(level: number, msg: string, exception?: any, extra?: any): void {
+ if (exception && !isException(exception)) {
+ extra = exception;
+ exception = undefined;
+ }
level = checkLevel(level);
if (this._isEnabledFor(level)) {
const now = new Date();
@@ -94,18 +100,19 @@ export class Logger extends Filterer implements ILogger {
levelName: LEVEL_NAMES[level] as string,
msg,
exception,
+ extra,
});
}
}
- public _isEnabledFor(level: number) {
+ private _isEnabledFor(level: number) {
if (this.manager && this.manager.disable > level) {
return false;
}
return level >= this._getParentLevel();
}
- public handle(record: IRecord) {
+ private handle(record: IRecord) {
if (this.filter(record)) {
let p = this as Logger;
while (p) {
@@ -123,7 +130,7 @@ export class Logger extends Filterer implements ILogger {
}
}
- public _getParentLevel() {
+ private _getParentLevel() {
let logger = this as Logger;
while (logger) {
if (logger.level) {
diff --git a/packages/logging/src/types.ts b/packages/logging/src/types.ts
index 102ef55..1e87a38 100644
--- a/packages/logging/src/types.ts
+++ b/packages/logging/src/types.ts
@@ -6,6 +6,7 @@ export interface IRecord {
readonly levelName?: string;
readonly msg?: string;
readonly exception?: any;
+ readonly extra?: any;
readonly timestamp: number;
readonly created: Date;
}
@@ -37,15 +38,15 @@ export interface ILogger {
removeHandler(handler: IHandler): ILogger;
hasHandlers(): boolean;
getHandlers(): IHandler[];
- fatal(msg: string, exception?: any): void;
- critical(msg: string, exception?: any): void;
- error(msg: string, exception?: any): void;
- warn(msg: string, exception?: any): void;
- warning(msg: string, exception?: any): void;
- info(msg: string): void;
- debug(msg: string): void;
- exception(exception: any): void;
- log(level: number, msg: string, exception?: any): void;
+ fatal(msg: string, exception?: any, extra?: any): void;
+ critical(msg: string, exception?: any, extra?: any): void;
+ error(msg: string, exception?: any, extra?: any): void;
+ warn(msg: string, exception?: any, extra?: any): void;
+ warning(msg: string, exception?: any, extra?: any): void;
+ info(msg: string, extra?: any): void;
+ debug(msg: string, extra?: any): void;
+ exception(exception: any, extra?: any): void;
+ log(level: number, msg: string, exception?: any, extra?: any): void;
}
export interface ILoggerFactory {
diff --git a/packages/logging/src/utils.ts b/packages/logging/src/utils.ts
index 84ad34f..3e5e015 100644
--- a/packages/logging/src/utils.ts
+++ b/packages/logging/src/utils.ts
@@ -16,6 +16,10 @@ export function checkLevel(level: number | string): number {
return Levels[level] as number;
}
}
- const msg = `Level not number or valid string: "${level}" [${NAMES}]`;
+ const msg = `Level is not number or valid string: "${level}" [${NAMES}]`;
throw TypeError(msg);
}
+
+export function isException(ex: any) {
+ return ex && ex.message && ex.stack;
+}
\ No newline at end of file
diff --git a/packages/logging/tests/format.test.ts b/packages/logging/tests/format.test.ts
index 81706e6..d1ea592 100644
--- a/packages/logging/tests/format.test.ts
+++ b/packages/logging/tests/format.test.ts
@@ -12,13 +12,16 @@ describe('@stool/logging :: formater', () => {
it('should generate simple format', () => {
let logger = new Logger('test');
const handler = new TestHandler();
- handler.setFormater(new SimpleFormater(SimpleFormater.SIMPLE));
+ handler.setFormater(new SimpleFormater(SimpleFormater.BASIC));
logger.addHandler(handler);
logger.info('info-1');
logger.debug('debug-1');
- assert.deepEqual(handler.events, ['test::INFO::info-1', 'test::DEBUG::debug-1']);
+ assert.deepEqual(handler.events, [
+ 'INFO|test|info-1',
+ 'DEBUG|test|debug-1',
+ ]);
});
it('should generate custom format', () => {
diff --git a/packages/logging/tsconfig.json b/packages/logging/tsconfig.json
index b5bdbee..863666d 100644
--- a/packages/logging/tsconfig.json
+++ b/packages/logging/tsconfig.json
@@ -12,6 +12,10 @@
"src/index.ts"
],
"ts-node": {
- "transpileOnly": true
+ "transpileOnly": true,
+ "moduleTypes": {
+ "src/*.ts": "cjs",
+ "tests/*.ts": "cjs"
+ }
}
}
\ No newline at end of file