Skip to content

Commit

Permalink
Fix karma tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smialy committed Mar 7, 2023
1 parent f775ddd commit 9ba9fdf
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
});
};
50 changes: 24 additions & 26 deletions packages/logging/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
}
]
}
2 changes: 1 addition & 1 deletion packages/logging/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -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]) {
Expand Down
8 changes: 4 additions & 4 deletions packages/logging/src/formaters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const DEFAULT_FORMATERS: TFormaters = {
return [
created.getUTCFullYear(),
'-',
pad10(created.getUTCMonth()+1),
pad10(created.getUTCMonth() + 1),
'-',
pad10(created.getUTCDate()),
'T',
Expand All @@ -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,
Expand All @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion packages/logging/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
41 changes: 24 additions & 17 deletions packages/logging/src/logger.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>;

export class Logger extends Filterer implements ILogger {
public manager?: any;
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
19 changes: 10 additions & 9 deletions packages/logging/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion packages/logging/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 5 additions & 2 deletions packages/logging/tests/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/logging/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"src/index.ts"
],
"ts-node": {
"transpileOnly": true
"transpileOnly": true,
"moduleTypes": {
"src/*.ts": "cjs",
"tests/*.ts": "cjs"
}
}
}

0 comments on commit 9ba9fdf

Please sign in to comment.