This commit is contained in:
Strix 2025-03-12 19:25:11 +01:00
parent 5215ba24cc
commit 6c3759c70d
12 changed files with 47 additions and 15 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
node_modules/
yarn.lock

13
package.json Normal file
View file

@ -0,0 +1,13 @@
{
"name": "tslog",
"version": "1.0.0",
"description": "Advanced TypeScript Logger",
"main": "src/index.ts",
"repository": "https://git.saluco.nl/strix/tslog.git",
"author": "Strix <strix@saluco.nl>",
"license": "MIT",
"dependencies": {
"@types/node": "^22.13.10",
"typescript": "^5.8.2"
}
}

View file

@ -4,5 +4,5 @@ export enum LogLevel {
Info, Info,
Warn, Warn,
Error, Error,
Fatal Critical
} }

View file

@ -1,4 +1,4 @@
import { Dispatch } from "./Dispatch"; import { Dispatch } from "./dispatch/Dispatch";
import { ILogItem } from "./ILogItem"; import { ILogItem } from "./ILogItem";
import { LogLevel } from "./LogLevel"; import { LogLevel } from "./LogLevel";
@ -15,7 +15,7 @@ export const LOGD = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(
export const LOGI = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Info, data: args }); export const LOGI = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Info, data: args });
export const LOGW = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Warn, data: args }); export const LOGW = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Warn, data: args });
export const LOGE = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Error, data: args }); export const LOGE = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Error, data: args });
export const LOGF = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Fatal, data: args }); export const LOGC = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Critical, data: args });
export const LOG = (item: Partial<ILogItem>) => CURRENT_DISPATCH.process({ export const LOG = (item: Partial<ILogItem>) => CURRENT_DISPATCH.process({
flags: item.flags ?? [], flags: item.flags ?? [],

View file

@ -1,6 +1,6 @@
import { IDispatch, IDispatchConfig } from "./IDispatch"; import { IDispatch, IDispatchConfig } from "./IDispatch";
import { ILogItem } from "./ILogItem"; import { ILogItem } from "../ILogItem";
import { LogLevel } from "./LogLevel"; import { LogLevel } from "../LogLevel";
export abstract class Dispatch implements IDispatch { export abstract class Dispatch implements IDispatch {
children: IDispatch[] = []; children: IDispatch[] = [];

View file

@ -1,8 +1,8 @@
import * as fs from "fs"; import * as fs from "fs";
import { FormattedDispatch } from "./FormattedDispatch"; import { FormattedDispatch } from "./FormattedDispatch";
import { App } from "../App"; import { App } from "../App";
import { ILogItem } from "./ILogItem"; import { ILogItem } from "../ILogItem";
import { LOGE, LOGW } from "./common"; import { LOGE, LOGW } from "../common";
export class FileDispatch extends FormattedDispatch { export class FileDispatch extends FormattedDispatch {
path: string; path: string;

View file

@ -1,7 +1,7 @@
import { object } from "zod"; import { object } from "zod";
import { Dispatch } from "./Dispatch"; import { Dispatch } from "./Dispatch";
import { ILogItem } from "./ILogItem"; import { ILogItem } from "../ILogItem";
import { LogLevel } from "./LogLevel"; import { LogLevel } from "../LogLevel";
export class FormattedDispatch extends Dispatch { export class FormattedDispatch extends Dispatch {
private formatString: string = "%t %l <%o>: %m"; private formatString: string = "%t %l <%o>: %m";

View file

@ -1,4 +1,4 @@
import { ILogItem } from "./ILogItem"; import { ILogItem } from "../ILogItem";
export interface IDispatchConfig {} export interface IDispatchConfig {}

View file

@ -1,7 +1,7 @@
import { statSync } from "fs"; import { statSync } from "fs";
import { Dispatch } from "./Dispatch"; import { Dispatch } from "./Dispatch";
import { ILogItem } from "./ILogItem"; import { ILogItem } from "../ILogItem";
import { LogLevel } from "./LogLevel"; import { LogLevel } from "../LogLevel";
export class SmartDispatch extends Dispatch { export class SmartDispatch extends Dispatch {
process(item: ILogItem) { process(item: ILogItem) {
@ -15,7 +15,7 @@ export class SmartDispatch extends Dispatch {
while (!stack[i].includes("at SmartDispatch.process")) i++; while (!stack[i].includes("at SmartDispatch.process")) i++;
let src = stack[i + 2].trim(); let src = stack[i + 2].trim();
let path = null; let path: string | null = null;
// now we need to format the src (at ...:..:.. or at App.<anonymous> ..., etc) // now we need to format the src (at ...:..:.. or at App.<anonymous> ..., etc)
if (src.includes("at ")) src = src.split("at ")[1]; if (src.includes("at ")) src = src.split("at ")[1];

View file

@ -1,7 +1,7 @@
import { FormattedDispatch } from "./FormattedDispatch"; import { FormattedDispatch } from "./FormattedDispatch";
import { IDispatch } from "./IDispatch"; import { IDispatch } from "./IDispatch";
import { ILogItem } from "./ILogItem"; import { ILogItem } from "../ILogItem";
import { LogLevel } from "./LogLevel"; import { LogLevel } from "../LogLevel";
export class StdoutDispatch extends FormattedDispatch { export class StdoutDispatch extends FormattedDispatch {
output(item: ILogItem) { output(item: ILogItem) {

17
src/index.ts Normal file
View file

@ -0,0 +1,17 @@
export {
LOG,
LOGT,
LOGD,
LOGI,
LOGW,
LOGE,
LOGC,
setDispatch
} from "./common";
export { Dispatch } from './dispatch/Dispatch';
export { IDispatch } from './dispatch/IDispatch';
export { FormattedDispatch } from './dispatch/FormattedDispatch';
export { SmartDispatch } from './dispatch/SmartDispatch';
export { FileDispatch } from './dispatch/FileDispatch';
export { StdoutDispatch } from './dispatch/StdoutDispatch';