Compare commits
25 Commits
alpha
...
d39e13dfe1
Author | SHA1 | Date | |
---|---|---|---|
d39e13dfe1 | |||
0742490527 | |||
c58d75129d | |||
249f701cb7 | |||
f01a4ffb21 | |||
43b94c5c75 | |||
9600b46699 | |||
b3f6a6c3f2 | |||
090aa4629b | |||
558ddad800 | |||
6f002456f6 | |||
a8a388997f | |||
2a55549199 | |||
c7c968753f | |||
c5098934a1 | |||
922af328a3 | |||
b76d0083b6 | |||
57f1b07944 | |||
a6a5eeded5 | |||
2735bcba35 | |||
4c656420ad | |||
cc73129373 | |||
9c454b403f | |||
5bbf0f74f5 | |||
8bf0d4b798 |
6
.editorconfig
Normal file
6
.editorconfig
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 3
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
end_of_line = lf
|
5561
package-lock.json
generated
5561
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
24
package.json
24
package.json
@ -1,16 +1,16 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/nodelogging",
|
"name": "@hibas123/nodelogging",
|
||||||
"version": "2.0.1",
|
"version": "3.0.6",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "out/index.js",
|
"main": "out/index.js",
|
||||||
"types": "out/index.d.ts",
|
"types": "out/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepublish": "tsc",
|
"prepublish": "npm run build",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"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",
|
||||||
"live": "nodemon out/test.js"
|
"live": "nodemon out/test.js"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
@ -19,14 +19,20 @@
|
|||||||
},
|
},
|
||||||
"author": "Fabian Stamm",
|
"author": "Fabian Stamm",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"files": [
|
||||||
|
"src/",
|
||||||
|
"out/",
|
||||||
|
"tsconfig.json",
|
||||||
|
"readme.md"
|
||||||
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^11.13.0",
|
"@types/node": "^15.0.2",
|
||||||
"concurrently": "^4.1.0",
|
"concurrently": "^6.0.2",
|
||||||
"nodemon": "^1.17.4",
|
"nodemon": "^2.0.7",
|
||||||
"typescript": "^3.4.1"
|
"typescript": "^4.2.4"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@hibas123/logging": "^2.0.0",
|
"@hibas123/logging": "^3.0.6",
|
||||||
"@hibas123/utils": "^2.0.5"
|
"@hibas123/utils": "^2.2.18"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -31,7 +31,7 @@ All Logging types except the simple error take as many arguments as you want. Th
|
|||||||
|
|
||||||
NodeLogging can work without any configuration, but it may be useful to change the log output folder.
|
NodeLogging can work without any configuration, but it may be useful to change the log output folder.
|
||||||
|
|
||||||
Todo so you are capable of creating own instances of the LoggingBase class
|
To do so you are capable of creating own instances of the LoggingBase class
|
||||||
|
|
||||||
``` javascript
|
``` javascript
|
||||||
const CustomLogging = new LoggingBase(name | {
|
const CustomLogging = new LoggingBase(name | {
|
||||||
@ -53,7 +53,7 @@ To not use any logfiles just set files to false.
|
|||||||
|
|
||||||
# Plugins
|
# Plugins
|
||||||
|
|
||||||
There is a new Plugin API available, that makes is possible to add custom Logging Adapter.
|
There is a Plugin API available, that makes is possible to add custom Logging Adapter.
|
||||||
|
|
||||||
``` javascript
|
``` javascript
|
||||||
const Demo = new LoggingExtended("Demo");
|
const Demo = new LoggingExtended("Demo");
|
||||||
|
@ -1,21 +1,19 @@
|
|||||||
import { Lock, ObservableInterface } from "@hibas123/utils";
|
import { 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 } from "@hibas123/logging";
|
||||||
|
|
||||||
|
|
||||||
const MAX_FILE_SIZE = 500000000;
|
const MAX_FILE_SIZE = 500000000;
|
||||||
|
|
||||||
export class LoggingFiles implements Adapter {
|
export class LoggingFiles implements Adapter {
|
||||||
file: Files;
|
file: Files;
|
||||||
constructor(filename: string, private error = false, private maxFileSize = MAX_FILE_SIZE) {
|
constructor(private filename: string, private maxFileSize = MAX_FILE_SIZE) {}
|
||||||
this.file = Files.getFile(filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
init() {
|
||||||
init(observable: ObservableInterface<Message>) {
|
if (!this.file) {
|
||||||
observable.subscribe(this.onMessage.bind(this));
|
this.file = Files.getFile(this.filename);
|
||||||
return this.file.init(this.maxFileSize);
|
this.file.init(this.maxFileSize);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
flush(sync: boolean) {
|
flush(sync: boolean) {
|
||||||
@ -23,21 +21,33 @@ export class LoggingFiles implements Adapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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 txt = message.text.formatted.map(fmt => fmt.map(f => f.text).join("") + "\n").join("");
|
|
||||||
|
|
||||||
let msg = Buffer.from(txt);
|
|
||||||
this.file.write(msg);
|
this.file.write(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
this.file.close();
|
this.file.close();
|
||||||
|
this.file = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: Optimise write path
|
||||||
|
|
||||||
|
const Debounce = (callback: () => void, iv = 500) => {
|
||||||
|
let to: any;
|
||||||
|
|
||||||
|
return {
|
||||||
|
trigger: () => {
|
||||||
|
if (!to) {
|
||||||
|
to = setTimeout(() => {
|
||||||
|
to = undefined;
|
||||||
|
callback();
|
||||||
|
}, iv);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export class Files {
|
export class Files {
|
||||||
private open = 0;
|
private open = 0;
|
||||||
|
|
||||||
@ -57,20 +67,24 @@ export class Files {
|
|||||||
private size: number = 0;
|
private size: number = 0;
|
||||||
private stream: fs.WriteStream = undefined;
|
private stream: fs.WriteStream = undefined;
|
||||||
private lock = new Lock();
|
private lock = new Lock();
|
||||||
|
private debounce = Debounce(() => this.checkQueue);
|
||||||
|
|
||||||
public initialized = false;
|
#initialized = false;
|
||||||
|
|
||||||
private constructor(private file: string) { }
|
public get initlialized() {
|
||||||
|
return this.#initialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
private constructor(private file: string) {}
|
||||||
|
|
||||||
public async init(maxFileSize: number) {
|
public async init(maxFileSize: number) {
|
||||||
if (this.initialized)
|
if (this.#initialized) return;
|
||||||
return;
|
|
||||||
let lock = await this.lock.getLock();
|
let lock = await this.lock.getLock();
|
||||||
this.maxFileSize == maxFileSize;
|
this.maxFileSize == maxFileSize;
|
||||||
await this.initializeFile()
|
await this.initializeFile();
|
||||||
this.initialized = true;
|
this.#initialized = true;
|
||||||
lock.release();
|
lock.release();
|
||||||
this.checkQueue()
|
this.checkQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async initializeFile(new_file = false) {
|
private async initializeFile(new_file = false) {
|
||||||
@ -80,8 +94,8 @@ export class Files {
|
|||||||
}
|
}
|
||||||
const folder = path.dirname(this.file);
|
const folder = path.dirname(this.file);
|
||||||
if (folder) {
|
if (folder) {
|
||||||
if (!await fsExists(folder)) {
|
if (!(await fsExists(folder))) {
|
||||||
await fsMkDir(folder).catch(() => { }); //Could happen, if two seperate instances want to create the same folder so ignoring
|
await fsMkDir(folder).catch(() => {}); //Could happen, if two seperate instances want to create the same folder so ignoring
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,17 +105,17 @@ export class Files {
|
|||||||
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");
|
||||||
} else {
|
} else {
|
||||||
size = stats.size;
|
size = stats.size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
//ToDo is this the right behavior?
|
//TODO: is this the right behavior?
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -112,7 +126,7 @@ export class Files {
|
|||||||
if (this.lock.locked) return;
|
if (this.lock.locked) return;
|
||||||
let lock = await this.lock.getLock();
|
let lock = await this.lock.getLock();
|
||||||
let msg: Buffer;
|
let msg: Buffer;
|
||||||
while (msg = this.queue.shift()) {
|
while ((msg = this.queue.shift())) {
|
||||||
await this.write_to_file(msg);
|
await this.write_to_file(msg);
|
||||||
}
|
}
|
||||||
lock.release();
|
lock.release();
|
||||||
@ -122,15 +136,16 @@ export class Files {
|
|||||||
await this.flush(false);
|
await this.flush(false);
|
||||||
this.open--;
|
this.open--;
|
||||||
if (this.open <= 0) {
|
if (this.open <= 0) {
|
||||||
this.stream.close()
|
this.stream.close();
|
||||||
Files.files.delete(this.file);
|
Files.files.delete(this.file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public flush(sync: boolean) {
|
public flush(sync: boolean) {
|
||||||
if (sync) {
|
if (sync) {
|
||||||
// if sync flush, the process most likely is in failstate, so checkQueue stopped its work.
|
// if sync flush, the process most likely is in failstate, so checkQueue stopped its work.
|
||||||
let msg: Buffer;
|
let msg: Buffer;
|
||||||
while (msg = this.queue.shift()) {
|
while ((msg = this.queue.shift())) {
|
||||||
this.stream.write(msg);
|
this.stream.write(msg);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -138,14 +153,17 @@ export class Files {
|
|||||||
const lock = await this.lock.getLock();
|
const lock = await this.lock.getLock();
|
||||||
lock.release();
|
lock.release();
|
||||||
await this.checkQueue();
|
await this.checkQueue();
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async write_to_file(data: Buffer) {
|
private async write_to_file(data: Buffer) {
|
||||||
try {
|
try {
|
||||||
if (data.byteLength < this.maxFileSize && this.size + data.byteLength > this.maxFileSize) {
|
if (
|
||||||
await this.initializeFile(true)
|
data.byteLength < this.maxFileSize &&
|
||||||
|
this.size + data.byteLength > this.maxFileSize
|
||||||
|
) {
|
||||||
|
await this.initializeFile(true);
|
||||||
}
|
}
|
||||||
this.size += data.byteLength;
|
this.size += data.byteLength;
|
||||||
this.stream.write(data);
|
this.stream.write(data);
|
||||||
@ -159,21 +177,19 @@ export class Files {
|
|||||||
|
|
||||||
public write(data: Buffer) {
|
public write(data: Buffer) {
|
||||||
this.queue.push(data);
|
this.queue.push(data);
|
||||||
this.checkQueue()
|
this.debounce.trigger();
|
||||||
}
|
}
|
||||||
|
|
||||||
public dispose() {
|
public dispose() {}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function fsUnlink(path) {
|
function fsUnlink(path) {
|
||||||
return new Promise((resolve, reject) => {
|
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();
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function fsStat(path: string) {
|
function fsStat(path: string) {
|
||||||
@ -181,36 +197,36 @@ function fsStat(path: string) {
|
|||||||
fs.stat(path, (err, stats) => {
|
fs.stat(path, (err, stats) => {
|
||||||
if (err) reject(err);
|
if (err) reject(err);
|
||||||
else resolve(stats);
|
else resolve(stats);
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
}
|
};
|
||||||
|
|
||||||
fs.rename(oldPath, newPath, function (err) {
|
fs.rename(oldPath, newPath, function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err.code === 'EXDEV') {
|
if (err.code === "EXDEV") {
|
||||||
copy();
|
copy();
|
||||||
} else {
|
} else {
|
||||||
callback(err)
|
callback(err);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
callback()
|
callback();
|
||||||
});
|
});
|
||||||
|
|
||||||
function copy() {
|
function copy() {
|
||||||
fs.copyFile(oldPath, newPath, (err) => {
|
fs.copyFile(oldPath, newPath, (err) => {
|
||||||
if (err) callback(err)
|
if (err) callback(err);
|
||||||
else fs.unlink(oldPath, callback);
|
else fs.unlink(oldPath, callback);
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function fsExists(path: string) {
|
function fsExists(path: string) {
|
||||||
@ -220,7 +236,7 @@ function fsExists(path: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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()));
|
||||||
});
|
});
|
||||||
}
|
}
|
74
src/index.ts
74
src/index.ts
@ -1,58 +1,28 @@
|
|||||||
export { LoggingFiles } from "./filewriter";
|
export { LoggingFiles } from "./filewriter";
|
||||||
import { LoggingFiles } from "./filewriter";
|
import { LoggingFiles } from "./filewriter";
|
||||||
import { LoggingBase as LoggingBaseOriginal, LoggingBaseOptions } from "@hibas123/logging";
|
import { LoggingBase } from "@hibas123/logging";
|
||||||
|
import Logging from "@hibas123/logging";
|
||||||
|
|
||||||
|
LoggingBase.nativeFunctions = {
|
||||||
export interface LoggingOptions extends LoggingBaseOptions {
|
startTimer: () => {
|
||||||
files: boolean | {
|
if (process.hrtime.bigint) {
|
||||||
/**
|
return process.hrtime.bigint();
|
||||||
* Filename/path of the logfile. Skip if generated with name.
|
} else {
|
||||||
*
|
return process.hrtime();
|
||||||
* 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));
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
endTimer: (start) => {
|
||||||
|
if (process.hrtime.bigint) {
|
||||||
|
return Number((process.hrtime.bigint() - start) / BigInt(1000)) / 1000;
|
||||||
|
} else {
|
||||||
|
let diff = process.hrtime(start);
|
||||||
|
return diff[0] * 1000 + diff[1] / 1000000;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const DefaultLoggingFile = new LoggingFiles("./logs/all.log");
|
||||||
|
|
||||||
|
Logging.addAdapter(DefaultLoggingFile);
|
||||||
|
|
||||||
export let Logging: LoggingBase = undefined;
|
|
||||||
if (process.env.LOGGING_NO_DEFAULT !== "true") {
|
|
||||||
Logging = new LoggingBase();
|
|
||||||
}
|
|
||||||
export default Logging;
|
export default Logging;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
86
src/test.ts
86
src/test.ts
@ -1,14 +1,17 @@
|
|||||||
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, { DefaultLoggingFile } from ".";
|
||||||
|
|
||||||
const deleteFolderRecursive = function (path: string) {
|
const deleteFolderRecursive = function (path: string) {
|
||||||
if (fs.existsSync(path)) {
|
if (fs.existsSync(path)) {
|
||||||
fs.readdirSync(path).forEach(function (file, index) {
|
fs.readdirSync(path).forEach(function (file, index) {
|
||||||
var curPath = path + "/" + file;
|
var curPath = path + "/" + file;
|
||||||
if (fs.lstatSync(curPath).isDirectory()) { // recurse
|
if (fs.lstatSync(curPath).isDirectory()) {
|
||||||
|
// recurse
|
||||||
deleteFolderRecursive(curPath);
|
deleteFolderRecursive(curPath);
|
||||||
} else { // delete file
|
} else {
|
||||||
|
// delete file
|
||||||
fs.unlinkSync(curPath);
|
fs.unlinkSync(curPath);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -16,61 +19,72 @@ const deleteFolderRecursive = function (path: string) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
deleteFolderRecursive("./logs")
|
deleteFolderRecursive("./logs");
|
||||||
|
|
||||||
Logging.log("test")
|
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("\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[31m\x1b[31m\x1b[31m")
|
Logging.log(
|
||||||
|
"\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[31m\x1b[31m\x1b[31m"
|
||||||
|
);
|
||||||
|
|
||||||
let err = new Error()
|
let err = new Error();
|
||||||
if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack)
|
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");
|
||||||
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");
|
||||||
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");
|
||||||
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");
|
||||||
cus2.log("Hello from custom Logger 2")
|
cus2.log("Hello from custom Logger 2");
|
||||||
|
|
||||||
const BenchmarkLogger = new LoggingBase({
|
const BenchmarkLogger = new LoggingBase({
|
||||||
console: false,
|
console: false,
|
||||||
name: "bench"
|
name: "bench",
|
||||||
})
|
});
|
||||||
|
|
||||||
|
BenchmarkLogger.addAdapter(DefaultLoggingFile);
|
||||||
|
|
||||||
async function benchmark(count: number, message_size: number) {
|
async function benchmark(count: number, message_size: number) {
|
||||||
await BenchmarkLogger.waitForSetup();
|
const randData = randomBytes(message_size).toString("hex");
|
||||||
const randData = randomBytes(message_size).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);
|
||||||
}
|
}
|
||||||
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(`Benchmark took ${ns / 1000000}ms for ${count} messages with a size of ${message_size} characters`);
|
console.log(
|
||||||
console.log(`This is equal to ${(ns / 1000000) / count} ms per message`)
|
`Benchmark took ${
|
||||||
|
ns / 1000000
|
||||||
|
}ms for ${count} messages with a size of ${message_size} characters`
|
||||||
|
);
|
||||||
|
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(7000, 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("Test Timer");
|
||||||
|
setTimeout(() => timer.end(), 1000);
|
||||||
|
Reference in New Issue
Block a user