Compare commits
17 Commits
558ddad800
...
master
Author | SHA1 | Date | |
---|---|---|---|
a399225c43 | |||
08fabf5bf8 | |||
6c65d2c83d | |||
ee3123f400 | |||
95ef923844 | |||
ccb5aa023f | |||
c372016397 | |||
ca8dffecff | |||
d39e13dfe1 | |||
0742490527 | |||
c58d75129d | |||
249f701cb7 | |||
f01a4ffb21 | |||
43b94c5c75 | |||
9600b46699 | |||
b3f6a6c3f2 | |||
090aa4629b |
4
.gitattributes
vendored
Normal file
4
.gitattributes
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
/.yarn/** linguist-vendored
|
||||||
|
/.yarn/releases/* binary
|
||||||
|
/.yarn/plugins/**/* binary
|
||||||
|
/.pnp.* binary linguist-generated
|
5
.gitignore
vendored
5
.gitignore
vendored
@ -2,4 +2,7 @@ node_modules/
|
|||||||
logs/
|
logs/
|
||||||
yarn.lock
|
yarn.lock
|
||||||
out/
|
out/
|
||||||
.history/
|
esm/
|
||||||
|
.history/
|
||||||
|
.yarn/cache
|
||||||
|
.yarn/install-state.gz
|
541
.yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
vendored
Normal file
541
.yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
vendored
Normal file
File diff suppressed because one or more lines are too long
874
.yarn/releases/yarn-3.6.4.cjs
vendored
Executable file
874
.yarn/releases/yarn-3.6.4.cjs
vendored
Executable file
File diff suppressed because one or more lines are too long
11
.yarnrc.yml
Normal file
11
.yarnrc.yml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
nodeLinker: node-modules
|
||||||
|
|
||||||
|
npmScopes:
|
||||||
|
"hibas123":
|
||||||
|
npmRegistryServer: "https://git.hibas.dev/api/packages/hibas123/npm/"
|
||||||
|
|
||||||
|
plugins:
|
||||||
|
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
|
||||||
|
spec: "@yarnpkg/plugin-interactive-tools"
|
||||||
|
|
||||||
|
yarnPath: .yarn/releases/yarn-3.6.4.cjs
|
3553
package-lock.json
generated
3553
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
26
package.json
26
package.json
@ -1,16 +1,19 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/nodelogging",
|
"name": "@hibas123/nodelogging",
|
||||||
"version": "2.3.5",
|
"packageManager": "yarn@3.6.4",
|
||||||
|
"version": "3.1.6",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "out/index.js",
|
"main": "out/index.js",
|
||||||
"types": "out/index.d.ts",
|
"types": "out/index.d.ts",
|
||||||
|
"module": "esm/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepublish": "tsc",
|
"prepublish": "npm run build",
|
||||||
"build": "tsc",
|
"build": "tsc && tsc -p tsconfig.esm.json",
|
||||||
"watch-ts": "tsc --watch",
|
"watch-ts": "tsc --watch",
|
||||||
"watch-js": "nodemon out/test.js",
|
"watch-js": "nodemon out/test.js",
|
||||||
"watch": "concurrently npm:watch-*",
|
"watch": "concurrently npm:watch-*",
|
||||||
"test": "node out/test.js",
|
"test": "npm run build && node out/test.js",
|
||||||
|
"benchmark": "npm run build && node out/benchmark.js",
|
||||||
"live": "nodemon out/test.js"
|
"live": "nodemon out/test.js"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
@ -22,17 +25,18 @@
|
|||||||
"files": [
|
"files": [
|
||||||
"src/",
|
"src/",
|
||||||
"out/",
|
"out/",
|
||||||
|
"esm/",
|
||||||
"tsconfig.json",
|
"tsconfig.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^13.11.0",
|
"@types/node": "^20.8.6",
|
||||||
"concurrently": "^5.1.0",
|
"concurrently": "^8.2.1",
|
||||||
"nodemon": "^2.0.2",
|
"nodemon": "^3.0.1",
|
||||||
"typescript": "^3.8.3"
|
"typescript": "^5.2.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@hibas123/logging": "^2.3.5",
|
"@hibas123/logging": "^3.1.2",
|
||||||
"@hibas123/utils": "^2.2.3"
|
"@hibas123/utils": "^2.2.18"
|
||||||
}
|
}
|
||||||
}
|
}
|
51
src/benchmark.ts
Normal file
51
src/benchmark.ts
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import { Formatted, LoggingBase, LoggingTypes } from "@hibas123/logging";
|
||||||
|
import { once } from "events";
|
||||||
|
import { createWriteStream } from "fs";
|
||||||
|
import { FileAdapter } from "./filewriter";
|
||||||
|
|
||||||
|
let results = {};
|
||||||
|
|
||||||
|
async function benchmark(
|
||||||
|
name: string,
|
||||||
|
count: number,
|
||||||
|
runner: (cnt: number) => Promise<void>
|
||||||
|
) {
|
||||||
|
console.log("Benchmark starting:", name);
|
||||||
|
const start = process.hrtime.bigint();
|
||||||
|
|
||||||
|
await runner(count);
|
||||||
|
|
||||||
|
const diffNS = process.hrtime.bigint() - start;
|
||||||
|
const diffMS = Number(diffNS / BigInt(1000 * 1000));
|
||||||
|
|
||||||
|
console.log("Benchmark ended:", name);
|
||||||
|
|
||||||
|
results[name] = {
|
||||||
|
count,
|
||||||
|
time: diffMS,
|
||||||
|
timePerI: (diffMS / count).toFixed(4),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Promise.resolve().then(async () => {
|
||||||
|
const largeText = "hallowelt!".repeat(250);
|
||||||
|
|
||||||
|
await benchmark("large data", 100000, async (cnt) => {
|
||||||
|
const lg = new LoggingBase({
|
||||||
|
console: false,
|
||||||
|
});
|
||||||
|
const fs = new FileAdapter("logs/benchmark", Number.MAX_SAFE_INTEGER);
|
||||||
|
await lg.addAdapter(fs);
|
||||||
|
|
||||||
|
console.time("Logging");
|
||||||
|
for (let i = 0; i < cnt; i++) {
|
||||||
|
lg.log(largeText);
|
||||||
|
}
|
||||||
|
console.timeEnd("Logging");
|
||||||
|
|
||||||
|
await fs.close();
|
||||||
|
await lg.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
console.table(results);
|
||||||
|
});
|
@ -1,54 +1,75 @@
|
|||||||
import { Lock, ObservableInterface } from "@hibas123/utils";
|
import { AwaitStore, Lock } from "@hibas123/utils";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import { Adapter, Message, LoggingTypes } from "@hibas123/logging";
|
import { Adapter, Message, Formatted, LoggingTypes } from "@hibas123/logging";
|
||||||
|
import { once } from "events";
|
||||||
|
|
||||||
const MAX_FILE_SIZE = 500000000;
|
const MAX_FILE_SIZE = 500000000;
|
||||||
|
|
||||||
export class LoggingFiles implements Adapter {
|
export class FileAdapter implements Adapter {
|
||||||
file: Files;
|
level = LoggingTypes.Debug;
|
||||||
constructor(
|
|
||||||
private filename: string,
|
|
||||||
private error = false,
|
|
||||||
private maxFileSize = MAX_FILE_SIZE,
|
|
||||||
private noPrefix = false
|
|
||||||
) {}
|
|
||||||
|
|
||||||
init(observable: ObservableInterface<Message>) {
|
file: Files;
|
||||||
observable.subscribe(this.onMessage.bind(this));
|
isInit = new AwaitStore(false);
|
||||||
|
constructor(private filename: string, private maxFileSize = MAX_FILE_SIZE) {}
|
||||||
|
|
||||||
|
setLevel(level: LoggingTypes) {
|
||||||
|
this.level = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
async init() {
|
||||||
if (!this.file) {
|
if (!this.file) {
|
||||||
this.file = Files.getFile(this.filename);
|
this.file = Files.getFile(this.filename);
|
||||||
return this.file.init(this.maxFileSize);
|
await this.file
|
||||||
|
.init(this.maxFileSize)
|
||||||
|
.then(() => this.isInit.send(true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
flush(sync: boolean) {
|
flush(sync: boolean) {
|
||||||
this.file.flush(sync);
|
// return this.file.flush(sync);
|
||||||
}
|
}
|
||||||
|
|
||||||
onMessage(message: Message) {
|
onMessage(message: Message) {
|
||||||
// Just ignore all non error messages, if this.error is set
|
let msg = Buffer.from(Formatted.strip(message.text) + "\n");
|
||||||
if (this.error && message.type !== LoggingTypes.Error) return;
|
|
||||||
|
|
||||||
let prefix = "";
|
|
||||||
if (message.name) prefix = `[${message.name}]=>`;
|
|
||||||
let txt = message.text.formatted
|
|
||||||
.map((fmt) => prefix + fmt.map((f) => f.text).join("") + "\n")
|
|
||||||
.join("");
|
|
||||||
|
|
||||||
let msg = Buffer.from(txt);
|
|
||||||
this.file.write(msg);
|
this.file.write(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
async close() {
|
||||||
this.file.close();
|
if (this.file) {
|
||||||
this.file = undefined;
|
await this.file.close();
|
||||||
|
this.file = undefined;
|
||||||
|
}
|
||||||
|
this.isInit.send(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Files {
|
//TODO: Optimise write path
|
||||||
private open = 0;
|
|
||||||
|
|
||||||
|
const Debounce = (callback: () => void, iv = 500, max = 100) => {
|
||||||
|
let to: any;
|
||||||
|
let curr = 0;
|
||||||
|
|
||||||
|
return {
|
||||||
|
trigger: () => {
|
||||||
|
curr++;
|
||||||
|
if (curr >= max) {
|
||||||
|
curr = 0; // not clearing timeout, since this is a very high cost operation
|
||||||
|
callback();
|
||||||
|
} else if (!to) {
|
||||||
|
to = setTimeout(() => {
|
||||||
|
to = undefined;
|
||||||
|
curr = 0;
|
||||||
|
callback();
|
||||||
|
}, iv);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const QUEUE_START_SIZE = 10000;
|
||||||
|
|
||||||
|
export class Files {
|
||||||
private static files = new Map<string, Files>();
|
private static files = new Map<string, Files>();
|
||||||
static getFile(filename: string): Files {
|
static getFile(filename: string): Files {
|
||||||
filename = path.resolve(filename);
|
filename = path.resolve(filename);
|
||||||
@ -61,45 +82,56 @@ export class Files {
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
private maxFileSize = MAX_FILE_SIZE;
|
private open = 0;
|
||||||
private size: number = 0;
|
|
||||||
private stream: fs.WriteStream = undefined;
|
|
||||||
private lock = new Lock();
|
|
||||||
|
|
||||||
private $initialized = false;
|
#maxFileSize = MAX_FILE_SIZE;
|
||||||
|
#size: number = 0;
|
||||||
|
#stream: fs.WriteStream = undefined;
|
||||||
|
#lock = new Lock();
|
||||||
|
#debounce = Debounce(this.checkQueue.bind(this));
|
||||||
|
#initialized = false;
|
||||||
|
#queue: Buffer[] = new Array(QUEUE_START_SIZE);
|
||||||
|
#queueIdx = 0;
|
||||||
|
|
||||||
public get initlialized() {
|
public get initlialized() {
|
||||||
return this.$initialized;
|
return this.#initialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
private constructor(private file: string) {}
|
private constructor(private file: string) {}
|
||||||
|
|
||||||
public async init(maxFileSize: number) {
|
public async init(maxFileSize: number) {
|
||||||
if (this.$initialized) return;
|
if (this.#initialized) return;
|
||||||
let lock = await this.lock.getLock();
|
this.#maxFileSize = maxFileSize;
|
||||||
this.maxFileSize == maxFileSize;
|
|
||||||
|
let lock = await this.#lock.getLock();
|
||||||
|
|
||||||
|
const folder = path.dirname(this.file);
|
||||||
|
if (folder) {
|
||||||
|
if (!(await fsExists(folder))) {
|
||||||
|
await fsMkDir(folder).catch(() => {}); //Could happen, if two seperate instances want to create the same folder so ignoring
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await this.initializeFile();
|
await this.initializeFile();
|
||||||
this.$initialized = true;
|
this.#initialized = true;
|
||||||
|
await this.checkQueue(true);
|
||||||
lock.release();
|
lock.release();
|
||||||
this.checkQueue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async initializeFile(new_file = false) {
|
private async initializeFile(new_file = false) {
|
||||||
try {
|
try {
|
||||||
if (this.stream) {
|
if (this.#stream) {
|
||||||
this.stream.close();
|
const closePrms = once(this.#stream, "close");
|
||||||
}
|
|
||||||
const folder = path.dirname(this.file);
|
this.#stream.end();
|
||||||
if (folder) {
|
|
||||||
if (!(await fsExists(folder))) {
|
await closePrms;
|
||||||
await fsMkDir(folder).catch(() => {}); //Could happen, if two seperate instances want to create the same folder so ignoring
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let size = 0;
|
let size = 0;
|
||||||
if (await fsExists(this.file)) {
|
if (await fsExists(this.file)) {
|
||||||
let stats = await fsStat(this.file);
|
let stats = await fsStat(this.file);
|
||||||
if (new_file || stats.size >= this.maxFileSize) {
|
if (new_file || stats.size >= this.#maxFileSize) {
|
||||||
if (await fsExists(this.file + ".old"))
|
if (await fsExists(this.file + ".old"))
|
||||||
await fsUnlink(this.file + ".old");
|
await fsUnlink(this.file + ".old");
|
||||||
await fsMove(this.file, this.file + ".old");
|
await fsMove(this.file, this.file + ".old");
|
||||||
@ -108,61 +140,80 @@ export class Files {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.stream = fs.createWriteStream(this.file, { flags: "a" });
|
this.#stream = fs.createWriteStream(this.file, { flags: "a" });
|
||||||
this.size = size;
|
this.#size = size;
|
||||||
} catch (e) {
|
} catch (err) {
|
||||||
console.log(e);
|
console.log(err);
|
||||||
//ToDo is this the right behavior?
|
//TODO: is this the right behavior? Probably not...
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private queue: Buffer[] = [];
|
private async checkQueue(nolock: boolean = false) {
|
||||||
|
let lock: any;
|
||||||
async checkQueue() {
|
if (nolock == false) {
|
||||||
if (this.lock.locked) return;
|
//TODO: New design might cause new messages to be "stalled" till close or another message
|
||||||
let lock = await this.lock.getLock();
|
if (this.#lock.locked) return;
|
||||||
let msg: Buffer;
|
lock = await this.#lock.getLock();
|
||||||
while ((msg = this.queue.shift())) {
|
|
||||||
await this.write_to_file(msg);
|
|
||||||
}
|
}
|
||||||
lock.release();
|
|
||||||
|
const queue = this.#queue;
|
||||||
|
const queueCnt = this.#queueIdx;
|
||||||
|
|
||||||
|
this.#queue = new Array(QUEUE_START_SIZE);
|
||||||
|
this.#queueIdx = 0;
|
||||||
|
|
||||||
|
let buffer = Buffer.alloc(1024 * 128);
|
||||||
|
let ci = 0;
|
||||||
|
for (let i = 0; i < queueCnt; i++) {
|
||||||
|
const entry = queue[i];
|
||||||
|
|
||||||
|
if (entry.length + ci > buffer.length) {
|
||||||
|
await this.write_to_file(buffer.slice(0, ci));
|
||||||
|
ci = 0;
|
||||||
|
if (entry.length > buffer.length) {
|
||||||
|
await this.write_to_file(entry);
|
||||||
|
} else {
|
||||||
|
entry.copy(buffer, ci);
|
||||||
|
ci += entry.length;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
entry.copy(buffer, ci);
|
||||||
|
ci += entry.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ci > 0) {
|
||||||
|
await this.write_to_file(buffer.slice(0, ci));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lock) lock.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async close() {
|
public async close() {
|
||||||
await this.flush(false);
|
//TODO: maybe some raceconditions when open collides with close
|
||||||
|
const lock = await this.#lock.getLock();
|
||||||
|
await this.checkQueue(true);
|
||||||
this.open--;
|
this.open--;
|
||||||
if (this.open <= 0) {
|
if (this.open <= 0) {
|
||||||
this.stream.close();
|
const a = once(this.#stream, "close");
|
||||||
|
this.#stream.close();
|
||||||
|
await a;
|
||||||
Files.files.delete(this.file);
|
Files.files.delete(this.file);
|
||||||
}
|
}
|
||||||
}
|
lock.release();
|
||||||
public flush(sync: boolean) {
|
|
||||||
if (sync) {
|
|
||||||
// if sync flush, the process most likely is in failstate, so checkQueue stopped its work.
|
|
||||||
let msg: Buffer;
|
|
||||||
while ((msg = this.queue.shift())) {
|
|
||||||
this.stream.write(msg);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Promise.resolve().then(async () => {
|
|
||||||
const lock = await this.lock.getLock();
|
|
||||||
lock.release();
|
|
||||||
await this.checkQueue();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async write_to_file(data: Buffer) {
|
private async write_to_file(data: Buffer) {
|
||||||
try {
|
try {
|
||||||
if (
|
if (
|
||||||
data.byteLength < this.maxFileSize &&
|
data.byteLength < this.#maxFileSize &&
|
||||||
this.size + data.byteLength > this.maxFileSize
|
this.#size + data.byteLength > this.#maxFileSize
|
||||||
) {
|
) {
|
||||||
await this.initializeFile(true);
|
await this.initializeFile(true);
|
||||||
}
|
}
|
||||||
this.size += data.byteLength;
|
this.#size += data.byteLength;
|
||||||
this.stream.write(data);
|
this.#stream.write(data);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// TODO: Better error handling!
|
// TODO: Better error handling!
|
||||||
console.error(err);
|
console.error(err);
|
||||||
@ -172,15 +223,16 @@ export class Files {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public write(data: Buffer) {
|
public write(data: Buffer) {
|
||||||
this.queue.push(data);
|
this.#queue[this.#queueIdx++] = data;
|
||||||
this.checkQueue();
|
this.#debounce.trigger();
|
||||||
}
|
}
|
||||||
|
|
||||||
public dispose() {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function fsUnlink(path) {
|
function fsUnlink(path: string) {
|
||||||
return new Promise((resolve, reject) => {
|
if (fs.promises?.unlink) {
|
||||||
|
return fs.promises.unlink(path);
|
||||||
|
}
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
fs.unlink(path, (err) => {
|
fs.unlink(path, (err) => {
|
||||||
if (err) reject(err);
|
if (err) reject(err);
|
||||||
else resolve();
|
else resolve();
|
||||||
@ -189,6 +241,9 @@ function fsUnlink(path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function fsStat(path: string) {
|
function fsStat(path: string) {
|
||||||
|
if (fs.promises?.stat) {
|
||||||
|
return fs.promises.stat(path);
|
||||||
|
}
|
||||||
return new Promise<fs.Stats>((resolve, reject) => {
|
return new Promise<fs.Stats>((resolve, reject) => {
|
||||||
fs.stat(path, (err, stats) => {
|
fs.stat(path, (err, stats) => {
|
||||||
if (err) reject(err);
|
if (err) reject(err);
|
||||||
@ -198,7 +253,7 @@ function fsStat(path: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function fsMove(oldPath: string, newPath: string) {
|
function fsMove(oldPath: string, newPath: string) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
let callback = (err?) => {
|
let callback = (err?) => {
|
||||||
if (err) reject(err);
|
if (err) reject(err);
|
||||||
else resolve();
|
else resolve();
|
||||||
@ -227,12 +282,12 @@ function fsMove(oldPath: string, newPath: string) {
|
|||||||
|
|
||||||
function fsExists(path: string) {
|
function fsExists(path: string) {
|
||||||
return new Promise<boolean>((resolve, reject) => {
|
return new Promise<boolean>((resolve, reject) => {
|
||||||
fs.exists(path, resolve);
|
fs.access(path, (err) => resolve(!err));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function fsMkDir(path: string) {
|
function fsMkDir(path: string) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
fs.mkdir(path, (err) => (err ? reject(err) : resolve()));
|
fs.mkdir(path, (err) => (err ? reject(err) : resolve()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
78
src/index.ts
78
src/index.ts
@ -1,78 +1,28 @@
|
|||||||
export { LoggingFiles } from "./filewriter";
|
export { FileAdapter } from "./filewriter";
|
||||||
import { LoggingFiles } from "./filewriter";
|
import { FileAdapter } from "./filewriter";
|
||||||
import {
|
import { LoggingBase } from "@hibas123/logging";
|
||||||
LoggingBase as LoggingBaseOriginal,
|
import Logging from "@hibas123/logging";
|
||||||
LoggingBaseOptions,
|
|
||||||
} from "@hibas123/logging";
|
|
||||||
|
|
||||||
export interface LoggingOptions extends LoggingBaseOptions {
|
LoggingBase.nativeFunctions = {
|
||||||
files:
|
startTimer: () => {
|
||||||
| boolean
|
|
||||||
| {
|
|
||||||
/**
|
|
||||||
* Filename/path of the logfile. Skip if generated with name.
|
|
||||||
*
|
|
||||||
* If not wanted pass null
|
|
||||||
*/
|
|
||||||
logfile?: string | null;
|
|
||||||
/**
|
|
||||||
* Filename/path of the logfile. Skip if generated with name.
|
|
||||||
*
|
|
||||||
* If not wanted pass null
|
|
||||||
*/
|
|
||||||
errorfile?: string | null;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export class LoggingBase extends LoggingBaseOriginal {
|
|
||||||
constructor(config: Partial<LoggingOptions> | string = {}) {
|
|
||||||
super(config);
|
|
||||||
|
|
||||||
if (typeof config === "string" || config.files !== false) {
|
|
||||||
let logfile: string;
|
|
||||||
let errorfile: string;
|
|
||||||
if (typeof config !== "string" && typeof config.files === "object") {
|
|
||||||
logfile = config.files.logfile;
|
|
||||||
errorfile = config.files.errorfile;
|
|
||||||
}
|
|
||||||
|
|
||||||
let name = this.name ? "." + this.name : "";
|
|
||||||
if (!logfile && logfile !== null) logfile = `./logs/all${name}.log`;
|
|
||||||
if (!errorfile && errorfile !== null)
|
|
||||||
errorfile = `./logs/error${name}.log`;
|
|
||||||
|
|
||||||
if (logfile) this.addAdapter(new LoggingFiles(logfile));
|
|
||||||
|
|
||||||
if (errorfile) this.addAdapter(new LoggingFiles(errorfile, true));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected postGetChild(child: LoggingBase) {
|
|
||||||
child.getCurrentTime = this.getCurrentTime.bind(child);
|
|
||||||
child.getTimeDiff = this.getTimeDiff.bind(child);
|
|
||||||
child.postGetChild = this.postGetChild.bind(child);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected getCurrentTime() {
|
|
||||||
if (process.hrtime.bigint) {
|
if (process.hrtime.bigint) {
|
||||||
return process.hrtime.bigint();
|
return process.hrtime.bigint();
|
||||||
} else {
|
} else {
|
||||||
return process.hrtime();
|
return process.hrtime();
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
endTimer: (start) => {
|
||||||
protected getTimeDiff(start) {
|
|
||||||
if (process.hrtime.bigint) {
|
if (process.hrtime.bigint) {
|
||||||
return Number((process.hrtime.bigint() - start) / BigInt(1000)) / 1000;
|
return Number((process.hrtime.bigint() - start) / BigInt(1000)) / 1000;
|
||||||
} else {
|
} else {
|
||||||
let diff = process.hrtime(start);
|
let diff = process.hrtime(start);
|
||||||
return diff[0] * 1000 + diff[1] / 1000000;
|
return diff[0] * 1000 + diff[1] / 1000000;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
|
|
||||||
|
export const DefaultFileAdapter = new FileAdapter("./logs/all.log");
|
||||||
|
|
||||||
|
Logging.addAdapter(DefaultFileAdapter);
|
||||||
|
|
||||||
export let Logging: LoggingBase = undefined;
|
|
||||||
if (process.env.LOGGING_NO_DEFAULT !== "true") {
|
|
||||||
Logging = new LoggingBase();
|
|
||||||
}
|
|
||||||
export default Logging;
|
export default Logging;
|
||||||
|
50
src/test.ts
50
src/test.ts
@ -1,6 +1,7 @@
|
|||||||
import { randomBytes } from "crypto";
|
import { randomBytes } from "crypto";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import { Logging, LoggingBase } from ".";
|
import { LoggingBase } from "@hibas123/logging";
|
||||||
|
import Logging, { DefaultFileAdapter, FileAdapter } from ".";
|
||||||
|
|
||||||
const deleteFolderRecursive = function (path: string) {
|
const deleteFolderRecursive = function (path: string) {
|
||||||
if (fs.existsSync(path)) {
|
if (fs.existsSync(path)) {
|
||||||
@ -24,7 +25,7 @@ Logging.log("test");
|
|||||||
Logging.log("i", "am", { a: "an" }, 1000);
|
Logging.log("i", "am", { a: "an" }, 1000);
|
||||||
Logging.error(new Error("fehler 001"));
|
Logging.error(new Error("fehler 001"));
|
||||||
Logging.debug("Some Debug infos");
|
Logging.debug("Some Debug infos");
|
||||||
Logging.errorMessage("i", "am", "an", "error");
|
Logging.error("i", "am", "an", "error");
|
||||||
|
|
||||||
Logging.log(
|
Logging.log(
|
||||||
"\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[31m\x1b[31m\x1b[31m"
|
"\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[31m\x1b[31m\x1b[31m"
|
||||||
@ -36,10 +37,10 @@ if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack);
|
|||||||
let cus = new LoggingBase({ name: "test" });
|
let cus = new LoggingBase({ name: "test" });
|
||||||
cus.log("Hello from custom Logger");
|
cus.log("Hello from custom Logger");
|
||||||
|
|
||||||
let cus2 = new LoggingBase("test2");
|
let cus2 = Logging.getChild("test2");
|
||||||
cus2.log("Hello from custom Logger 2");
|
cus2.log("Hello from custom Logger 2");
|
||||||
|
|
||||||
let cus22 = new LoggingBase("test2");
|
let cus22 = Logging.getChild("test2");
|
||||||
cus22.log("Hello from custom Logger 22");
|
cus22.log("Hello from custom Logger 22");
|
||||||
cus2.log("Hello from custom Logger 2");
|
cus2.log("Hello from custom Logger 2");
|
||||||
cus22.log("Hello from custom Logger 22");
|
cus22.log("Hello from custom Logger 22");
|
||||||
@ -51,20 +52,34 @@ cus2.log("Hello from custom Logger 2");
|
|||||||
cus22.log("Hello from custom Logger 22");
|
cus22.log("Hello from custom Logger 22");
|
||||||
cus2.log("Hello from custom Logger 2");
|
cus2.log("Hello from custom Logger 2");
|
||||||
|
|
||||||
const BenchmarkLogger = new LoggingBase({
|
|
||||||
console: false,
|
|
||||||
name: "bench",
|
|
||||||
});
|
|
||||||
async function benchmark(count: number, message_size: number) {
|
async function benchmark(count: number, message_size: number) {
|
||||||
await BenchmarkLogger.waitForSetup();
|
const BenchmarkLogger = new LoggingBase({
|
||||||
const randData = randomBytes(message_size).toString("hex");
|
console: false,
|
||||||
|
name: "bench",
|
||||||
|
});
|
||||||
|
|
||||||
|
if (fs.existsSync("logs/benchmark")) {
|
||||||
|
fs.unlinkSync("logs/benchmark");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fs.existsSync("logs/benchmark.old")) {
|
||||||
|
fs.unlinkSync("logs/benchmark.old");
|
||||||
|
}
|
||||||
|
|
||||||
|
const BenchmarkFile = new FileAdapter("logs/benchmark");
|
||||||
|
|
||||||
|
BenchmarkLogger.addAdapter(BenchmarkFile);
|
||||||
|
|
||||||
|
const randData = randomBytes(message_size / 2).toString("hex");
|
||||||
const t = process.hrtime();
|
const t = process.hrtime();
|
||||||
for (let i = 0; i < count; i++) {
|
for (let i = 0; i < count; i++) {
|
||||||
BenchmarkLogger.log(randData);
|
BenchmarkLogger.log(randData);
|
||||||
}
|
}
|
||||||
|
await BenchmarkFile.flush(false);
|
||||||
|
await BenchmarkLogger.close();
|
||||||
|
|
||||||
const diff = process.hrtime(t);
|
const diff = process.hrtime(t);
|
||||||
const NS_PER_SEC = 1e9;
|
const NS_PER_SEC = 1e9;
|
||||||
await BenchmarkLogger.waitForSetup();
|
|
||||||
const ns = diff[0] * NS_PER_SEC + diff[1];
|
const ns = diff[0] * NS_PER_SEC + diff[1];
|
||||||
console.log(
|
console.log(
|
||||||
`Benchmark took ${
|
`Benchmark took ${
|
||||||
@ -74,14 +89,15 @@ async function benchmark(count: number, message_size: number) {
|
|||||||
console.log(`This is equal to ${ns / 1000000 / count} ms per message`);
|
console.log(`This is equal to ${ns / 1000000 / count} ms per message`);
|
||||||
}
|
}
|
||||||
|
|
||||||
Logging.waitForSetup().then(async () => {
|
const benchTimer = Logging.time("benchmark");
|
||||||
return;
|
Promise.resolve().then(async () => {
|
||||||
console.log("Large data benchmark:");
|
console.log("Large data benchmark:");
|
||||||
await benchmark(7000, 50000);
|
await benchmark(70000, 50000);
|
||||||
|
|
||||||
console.log("Realdata data benchmark:");
|
console.log("Realdata data benchmark:");
|
||||||
await benchmark(100000, 100);
|
await benchmark(100000, 100);
|
||||||
});
|
benchTimer.end();
|
||||||
|
|
||||||
const timer = Logging.time("timer1", "Test Timer");
|
const timer = Logging.time("Test Timer");
|
||||||
setTimeout(() => timer.end(), 1000);
|
setTimeout(() => timer.end(), 1000);
|
||||||
|
});
|
||||||
|
11
tsconfig.esm.json
Normal file
11
tsconfig.esm.json
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"extends": "./tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "ESNext",
|
||||||
|
"target": "ES2017",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"outDir": "esm"
|
||||||
|
},
|
||||||
|
"exclude": ["node_modules"],
|
||||||
|
"include": ["src"]
|
||||||
|
}
|
Reference in New Issue
Block a user