mirror of
https://git.stamm.me/OpenServer/NodeLogging.git
synced 2024-11-14 17:11:04 +00:00
V3
This commit is contained in:
parent
f01a4ffb21
commit
249f701cb7
2752
package-lock.json
generated
2752
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
16
package.json
16
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/nodelogging",
|
||||
"version": "2.4.4",
|
||||
"version": "3.0.0",
|
||||
"description": "",
|
||||
"main": "out/index.js",
|
||||
"types": "out/index.d.ts",
|
||||
@ -10,7 +10,7 @@
|
||||
"watch-ts": "tsc --watch",
|
||||
"watch-js": "nodemon out/test.js",
|
||||
"watch": "concurrently npm:watch-*",
|
||||
"test": "node out/test.js",
|
||||
"test": "npm run build && node out/test.js",
|
||||
"live": "nodemon out/test.js"
|
||||
},
|
||||
"repository": {
|
||||
@ -26,13 +26,13 @@
|
||||
"readme.md"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@types/node": "^13.13.4",
|
||||
"concurrently": "^5.2.0",
|
||||
"nodemon": "^2.0.3",
|
||||
"typescript": "^3.8.3"
|
||||
"@types/node": "^15.0.2",
|
||||
"concurrently": "^6.0.2",
|
||||
"nodemon": "^2.0.7",
|
||||
"typescript": "^4.2.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@hibas123/logging": "^2.4.4",
|
||||
"@hibas123/utils": "^2.2.4"
|
||||
"@hibas123/logging": "^3.0.0",
|
||||
"@hibas123/utils": "^2.2.18"
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Lock, ObservableInterface } from "@hibas123/utils";
|
||||
import { Lock } from "@hibas123/utils";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import { Adapter, Message, LoggingTypes } from "@hibas123/logging";
|
||||
import { Adapter, Message, Formatted } from "@hibas123/logging";
|
||||
|
||||
const MAX_FILE_SIZE = 500000000;
|
||||
|
||||
@ -14,11 +14,10 @@ export class LoggingFiles implements Adapter {
|
||||
private noPrefix = false
|
||||
) {}
|
||||
|
||||
init(observable: ObservableInterface<Message>) {
|
||||
observable.subscribe(this.onMessage.bind(this));
|
||||
init() {
|
||||
if (!this.file) {
|
||||
this.file = Files.getFile(this.filename);
|
||||
return this.file.init(this.maxFileSize);
|
||||
this.file.init(this.maxFileSize);
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,16 +26,7 @@ export class LoggingFiles implements Adapter {
|
||||
}
|
||||
|
||||
onMessage(message: Message) {
|
||||
// Just ignore all non error messages, if this.error is set
|
||||
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);
|
||||
let msg = Buffer.from(Formatted.strip(message.text) + "\n");
|
||||
this.file.write(msg);
|
||||
}
|
||||
|
||||
@ -46,6 +36,8 @@ export class LoggingFiles implements Adapter {
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Optimise write path
|
||||
|
||||
export class Files {
|
||||
private open = 0;
|
||||
|
||||
@ -180,7 +172,7 @@ export class Files {
|
||||
}
|
||||
|
||||
function fsUnlink(path) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
fs.unlink(path, (err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
@ -198,7 +190,7 @@ function fsStat(path: string) {
|
||||
}
|
||||
|
||||
function fsMove(oldPath: string, newPath: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
let callback = (err?) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
@ -232,7 +224,7 @@ function fsExists(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()));
|
||||
});
|
||||
}
|
||||
|
74
src/index.ts
74
src/index.ts
@ -1,78 +1,28 @@
|
||||
export { LoggingFiles } from "./filewriter";
|
||||
import { LoggingFiles } from "./filewriter";
|
||||
import {
|
||||
LoggingBase as LoggingBaseOriginal,
|
||||
LoggingBaseOptions,
|
||||
} from "@hibas123/logging";
|
||||
import { Logging as LoggingBase } from "@hibas123/logging";
|
||||
import Logging from "@hibas123/logging";
|
||||
|
||||
export interface LoggingOptions extends LoggingBaseOptions {
|
||||
files:
|
||||
| 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() {
|
||||
LoggingBase.nativeFunctions = {
|
||||
startTimer: () => {
|
||||
if (process.hrtime.bigint) {
|
||||
return process.hrtime.bigint();
|
||||
} else {
|
||||
return process.hrtime();
|
||||
}
|
||||
}
|
||||
|
||||
protected getTimeDiff(start) {
|
||||
},
|
||||
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;
|
||||
|
21
src/test.ts
21
src/test.ts
@ -1,6 +1,7 @@
|
||||
import { randomBytes } from "crypto";
|
||||
import * as fs from "fs";
|
||||
import { Logging, LoggingBase } from ".";
|
||||
import { Logging as LoggingBase } from "@hibas123/logging";
|
||||
import Logging, { DefaultLoggingFile } from ".";
|
||||
|
||||
const deleteFolderRecursive = function (path: string) {
|
||||
if (fs.existsSync(path)) {
|
||||
@ -24,7 +25,7 @@ Logging.log("test");
|
||||
Logging.log("i", "am", { a: "an" }, 1000);
|
||||
Logging.error(new Error("fehler 001"));
|
||||
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"
|
||||
@ -36,10 +37,10 @@ if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack);
|
||||
let cus = new LoggingBase({ name: "test" });
|
||||
cus.log("Hello from custom Logger");
|
||||
|
||||
let cus2 = new LoggingBase("test2");
|
||||
let cus2 = Logging.getChild("test2");
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
|
||||
let cus22 = new LoggingBase("test2");
|
||||
let cus22 = Logging.getChild("test2");
|
||||
cus22.log("Hello from custom Logger 22");
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
cus22.log("Hello from custom Logger 22");
|
||||
@ -55,8 +56,10 @@ const BenchmarkLogger = new LoggingBase({
|
||||
console: false,
|
||||
name: "bench",
|
||||
});
|
||||
|
||||
BenchmarkLogger.addAdapter(DefaultLoggingFile);
|
||||
|
||||
async function benchmark(count: number, message_size: number) {
|
||||
await BenchmarkLogger.waitForSetup();
|
||||
const randData = randomBytes(message_size).toString("hex");
|
||||
const t = process.hrtime();
|
||||
for (let i = 0; i < count; i++) {
|
||||
@ -64,7 +67,6 @@ async function benchmark(count: number, message_size: number) {
|
||||
}
|
||||
const diff = process.hrtime(t);
|
||||
const NS_PER_SEC = 1e9;
|
||||
await BenchmarkLogger.waitForSetup();
|
||||
const ns = diff[0] * NS_PER_SEC + diff[1];
|
||||
console.log(
|
||||
`Benchmark took ${
|
||||
@ -74,14 +76,15 @@ async function benchmark(count: number, message_size: number) {
|
||||
console.log(`This is equal to ${ns / 1000000 / count} ms per message`);
|
||||
}
|
||||
|
||||
Logging.waitForSetup().then(async () => {
|
||||
return;
|
||||
const benchTimer = Logging.time("benchmark");
|
||||
Promise.resolve().then(async () => {
|
||||
console.log("Large data benchmark:");
|
||||
await benchmark(7000, 50000);
|
||||
|
||||
console.log("Realdata data benchmark:");
|
||||
await benchmark(100000, 100);
|
||||
benchTimer.end();
|
||||
});
|
||||
|
||||
const timer = Logging.time("timer1", "Test Timer");
|
||||
const timer = Logging.time("Test Timer");
|
||||
setTimeout(() => timer.end(), 1000);
|
||||
|
Loading…
Reference in New Issue
Block a user