@4players/odin-common 2.4.2 → 2.4.4

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.
package/lib/cjs/index.js CHANGED
@@ -36,4 +36,5 @@ __exportStar(require("./utility/strand"), exports);
36
36
  __exportStar(require("./utility/url"), exports);
37
37
  __exportStar(require("./utility/uuid"), exports);
38
38
  __exportStar(require("./utility/validation"), exports);
39
+ __exportStar(require("./utility/log"), exports);
39
40
  __exportStar(require("./plugin/api"), exports);
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Logger = exports.log = exports.OdinError = void 0;
4
+ class OdinError extends Error {
5
+ constructor(message = '', options) {
6
+ super(message);
7
+ this.message = message;
8
+ this.name = 'OdinError';
9
+ const date = new Date();
10
+ const hours = date.getHours();
11
+ const minutes = date.getMinutes();
12
+ const seconds = date.getSeconds();
13
+ if (options === null || options === void 0 ? void 0 : options.project) {
14
+ this.message = `ODIN ${options === null || options === void 0 ? void 0 : options.project} | ${options === null || options === void 0 ? void 0 : options.kind}`;
15
+ }
16
+ this.message += ` at ${options === null || options === void 0 ? void 0 : options.project} ${hours}:${minutes}:${seconds}\n`;
17
+ this.message += `${message}\n`;
18
+ }
19
+ }
20
+ exports.OdinError = OdinError;
21
+ function log(message, options, filters = { projects: [], kind: [] }) {
22
+ for (const projectName of filters.projects) {
23
+ if (options.project === projectName) {
24
+ for (const kindName of filters.kind) {
25
+ if (options.kind === kindName) {
26
+ let logMessage = '';
27
+ const date = new Date();
28
+ const hours = date.getHours();
29
+ const minutes = date.getMinutes();
30
+ const seconds = date.getSeconds();
31
+ if (options === null || options === void 0 ? void 0 : options.project) {
32
+ logMessage = `ODIN ${options === null || options === void 0 ? void 0 : options.project} | ${options === null || options === void 0 ? void 0 : options.kind}`;
33
+ }
34
+ logMessage += ` at ${options === null || options === void 0 ? void 0 : options.project} ${hours}:${minutes}:${seconds}\n`;
35
+ logMessage += `${message}\n`;
36
+ if (options.type) {
37
+ console[options.type](logMessage);
38
+ }
39
+ else {
40
+ console.log(logMessage);
41
+ }
42
+ }
43
+ }
44
+ }
45
+ }
46
+ }
47
+ exports.log = log;
48
+ class Logger {
49
+ constructor(project, filters) {
50
+ this.project = project;
51
+ this.filters = filters;
52
+ }
53
+ log(message, kind) {
54
+ log('message', { project: this.project, kind, type: 'log' }, this.filters);
55
+ }
56
+ info(message, kind) {
57
+ log('message', { project: this.project, kind, type: 'info' }, this.filters);
58
+ }
59
+ error(message, kind) {
60
+ log('message', { project: this.project, kind, type: 'error' }, this.filters);
61
+ }
62
+ warn(message, kind) {
63
+ log('message', { project: this.project, kind, type: 'warn' }, this.filters);
64
+ }
65
+ debug(message, kind) {
66
+ log('message', { project: this.project, kind, type: 'debug' }, this.filters);
67
+ }
68
+ }
69
+ exports.Logger = Logger;
@@ -2,14 +2,15 @@
2
2
  /* eslint-disable no-redeclare */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.unwrap = exports.failure = exports.success = exports.fail = exports.assert = void 0;
5
+ const log_1 = require("./log");
5
6
  function assert(condition, message) {
6
7
  if (!condition) {
7
8
  fail(message);
8
9
  }
9
10
  }
10
11
  exports.assert = assert;
11
- function fail(message) {
12
- throw new Error(message);
12
+ function fail(message, options) {
13
+ throw new log_1.OdinError(message, options);
13
14
  }
14
15
  exports.fail = fail;
15
16
  function success(value) {
package/lib/esm/index.js CHANGED
@@ -20,4 +20,5 @@ export * from './utility/strand';
20
20
  export * from './utility/url';
21
21
  export * from './utility/uuid';
22
22
  export * from './utility/validation';
23
+ export * from './utility/log';
23
24
  export * from './plugin/api';
@@ -0,0 +1,63 @@
1
+ export class OdinError extends Error {
2
+ constructor(message = '', options) {
3
+ super(message);
4
+ this.message = message;
5
+ this.name = 'OdinError';
6
+ const date = new Date();
7
+ const hours = date.getHours();
8
+ const minutes = date.getMinutes();
9
+ const seconds = date.getSeconds();
10
+ if (options === null || options === void 0 ? void 0 : options.project) {
11
+ this.message = `ODIN ${options === null || options === void 0 ? void 0 : options.project} | ${options === null || options === void 0 ? void 0 : options.kind}`;
12
+ }
13
+ this.message += ` at ${options === null || options === void 0 ? void 0 : options.project} ${hours}:${minutes}:${seconds}\n`;
14
+ this.message += `${message}\n`;
15
+ }
16
+ }
17
+ export function log(message, options, filters = { projects: [], kind: [] }) {
18
+ for (const projectName of filters.projects) {
19
+ if (options.project === projectName) {
20
+ for (const kindName of filters.kind) {
21
+ if (options.kind === kindName) {
22
+ let logMessage = '';
23
+ const date = new Date();
24
+ const hours = date.getHours();
25
+ const minutes = date.getMinutes();
26
+ const seconds = date.getSeconds();
27
+ if (options === null || options === void 0 ? void 0 : options.project) {
28
+ logMessage = `ODIN ${options === null || options === void 0 ? void 0 : options.project} | ${options === null || options === void 0 ? void 0 : options.kind}`;
29
+ }
30
+ logMessage += ` at ${options === null || options === void 0 ? void 0 : options.project} ${hours}:${minutes}:${seconds}\n`;
31
+ logMessage += `${message}\n`;
32
+ if (options.type) {
33
+ console[options.type](logMessage);
34
+ }
35
+ else {
36
+ console.log(logMessage);
37
+ }
38
+ }
39
+ }
40
+ }
41
+ }
42
+ }
43
+ export class Logger {
44
+ constructor(project, filters) {
45
+ this.project = project;
46
+ this.filters = filters;
47
+ }
48
+ log(message, kind) {
49
+ log('message', { project: this.project, kind, type: 'log' }, this.filters);
50
+ }
51
+ info(message, kind) {
52
+ log('message', { project: this.project, kind, type: 'info' }, this.filters);
53
+ }
54
+ error(message, kind) {
55
+ log('message', { project: this.project, kind, type: 'error' }, this.filters);
56
+ }
57
+ warn(message, kind) {
58
+ log('message', { project: this.project, kind, type: 'warn' }, this.filters);
59
+ }
60
+ debug(message, kind) {
61
+ log('message', { project: this.project, kind, type: 'debug' }, this.filters);
62
+ }
63
+ }
@@ -1,11 +1,12 @@
1
1
  /* eslint-disable no-redeclare */
2
+ import { OdinError } from './log';
2
3
  export function assert(condition, message) {
3
4
  if (!condition) {
4
5
  fail(message);
5
6
  }
6
7
  }
7
- export function fail(message) {
8
- throw new Error(message);
8
+ export function fail(message, options) {
9
+ throw new OdinError(message, options);
9
10
  }
10
11
  export function success(value) {
11
12
  return { type: 'Success', value };
package/lib/index.d.ts CHANGED
@@ -20,4 +20,5 @@ export * from './utility/strand';
20
20
  export * from './utility/url';
21
21
  export * from './utility/uuid';
22
22
  export * from './utility/validation';
23
+ export * from './utility/log';
23
24
  export * from './plugin/api';
@@ -0,0 +1,26 @@
1
+ export type LogType = 'debug' | 'info' | 'warn' | 'error' | 'log';
2
+ export type LogOptions = {
3
+ kind: string;
4
+ project: string;
5
+ type?: LogType;
6
+ };
7
+ export type LogFilters = {
8
+ projects: [];
9
+ kind: [];
10
+ };
11
+ export declare class OdinError extends Error {
12
+ message: string;
13
+ readonly name = "OdinError";
14
+ constructor(message?: string, options?: LogOptions);
15
+ }
16
+ export declare function log(message: string, options: LogOptions, filters?: LogFilters): void;
17
+ export declare class Logger {
18
+ readonly project: string;
19
+ filters: LogFilters;
20
+ constructor(project: string, filters: LogFilters);
21
+ log(message: string, kind: string): void;
22
+ info(message: string, kind: string): void;
23
+ error(message: string, kind: string): void;
24
+ warn(message: string, kind: string): void;
25
+ debug(message: string, kind: string): void;
26
+ }
@@ -1,5 +1,6 @@
1
+ import { LogOptions } from './log';
1
2
  export declare function assert(condition: unknown, message: string): asserts condition;
2
- export declare function fail(message: string): never;
3
+ export declare function fail(message: string, options?: LogOptions): never;
3
4
  export type Failure = {
4
5
  type: 'Failure';
5
6
  reason: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4players/odin-common",
3
- "version": "2.4.2",
3
+ "version": "2.4.4",
4
4
  "description": "A collection of commonly used type definitions and utility functions across ODIN web projects",
5
5
  "author": "Josho Bleicker <josho.bleicker@4players.io> (https://www.4players.io)",
6
6
  "homepage": "https://www.4players.io",
@@ -43,4 +43,4 @@
43
43
  "testyts": "~1.5.0",
44
44
  "typescript": "~5.4.0"
45
45
  }
46
- }
46
+ }