From 8e7c381863fd31b74fece5c3249b3a35277d6309 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Wed, 23 Aug 2017 16:45:03 +0200 Subject: [PATCH] First Commit --- .gitignore | 2 + out/index.d.ts | 24 ++++++ out/index.js | 182 +++++++++++++++++++++++++++++++++++++++++++++ out/index.js.map | 1 + out/test.d.ts | 0 out/test.js | 17 +++++ out/test.js.map | 1 + package.json | 20 +++++ src/index.ts | 188 +++++++++++++++++++++++++++++++++++++++++++++++ src/test.ts | 7 ++ tsconfig.json | 19 +++++ yarn.lock | 11 +++ 12 files changed, 472 insertions(+) create mode 100644 .gitignore create mode 100644 out/index.d.ts create mode 100644 out/index.js create mode 100644 out/index.js.map create mode 100644 out/test.d.ts create mode 100644 out/test.js create mode 100644 out/test.js.map create mode 100644 package.json create mode 100644 src/index.ts create mode 100644 src/test.ts create mode 100644 tsconfig.json create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a6693ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +logs/ \ No newline at end of file diff --git a/out/index.d.ts b/out/index.d.ts new file mode 100644 index 0000000..f9ef1e9 --- /dev/null +++ b/out/index.d.ts @@ -0,0 +1,24 @@ +export declare class Logging { + private static logFileLocation; + private static stdout; + private static fileStream; + private static errorStream; + private static writing; + private static queue; + static config(logfolder: string, stdout: boolean): void; + static debug(...message: any[]): void; + static log(...message: any[]): void; + static warning(...message: any[]): void; + static error(error: Error | string): void; + static errorMessage(...message: string[]): void; + private static message(type, message); + private static writeMessageToFile(message, error?); + private static checkQueue(); + private static initializeFile(); +} +export declare enum LoggingTypes { + Log = 0, + Warning = 1, + Error = 2, + Debug = 3, +} diff --git a/out/index.js b/out/index.js new file mode 100644 index 0000000..c297d55 --- /dev/null +++ b/out/index.js @@ -0,0 +1,182 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const util = require("util"); +const fs = require("fs"); +const Reset = "\x1b[0m"; +const Bright = "\x1b[1m"; +const Dim = "\x1b[2m"; +const Underscore = "\x1b[4m"; +const Blink = "\x1b[5m"; +const Reverse = "\x1b[7m"; +const Hidden = "\x1b[8m"; +const FgBlack = "\x1b[30m"; +const FgRed = "\x1b[31m"; +const FgGreen = "\x1b[32m"; +const FgYellow = "\x1b[33m"; +const FgBlue = "\x1b[34m"; +const FgMagenta = "\x1b[35m"; +const FgCyan = "\x1b[36m"; +const FgWhite = "\x1b[37m"; +const BgBlack = "\x1b[40m"; +const BgRed = "\x1b[41m"; +const BgGreen = "\x1b[42m"; +const BgYellow = "\x1b[43m"; +const BgBlue = "\x1b[44m"; +const BgMagenta = "\x1b[45m"; +const BgCyan = "\x1b[46m"; +const BgWhite = "\x1b[47m"; +class Logging { + static config(logfolder, stdout) { + this.logFileLocation = logfolder; + this.stdout = stdout; + } + static debug(...message) { + Logging.message(LoggingTypes.Debug, message); + } + static log(...message) { + Logging.message(LoggingTypes.Log, message); + } + static warning(...message) { + Logging.message(LoggingTypes.Log, message); + } + static error(error) { + if (typeof error === "string") { + Logging.errorMessage(error); + return; + } + var message = error.name + " " + error.message + "\n" + error.stack; + Logging.message(LoggingTypes.Error, [message]); + } + static errorMessage(...message) { + Logging.message(LoggingTypes.Error, message); + } + static async message(type, message) { + var consoleLogFormat = Reset; + switch (type) { + case LoggingTypes.Log: + //m += FgWhite + BgBlack; + break; + case LoggingTypes.Error: + consoleLogFormat += FgRed; //FgWhite + BgRed + FgWhite; + break; + case LoggingTypes.Debug: + consoleLogFormat += FgCyan; + break; + case LoggingTypes.Warning: + consoleLogFormat += FgYellow; + break; + } + var mb = ""; + if (typeof message === "string") { + mb = message; + } + else { + message.forEach(e => { + if (typeof e !== "string") + e = util.inspect(e, false, null); + if (e.endsWith("\n")) { + mb += e; + } + else { + mb += e + " "; + } + }); + } + var m = "[" + LoggingTypes[type] + "][" + _getCallerFile() + "][" + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') + "]: " + mb; + if (this.logFileLocation) { + if (!this.fileStream || !this.errorStream) { + await Logging.initializeFile(); + } + Logging.writeMessageToFile(m, type === LoggingTypes.Error); + } + if (this.stdout) + console.log(consoleLogFormat + m + Reset); + } + static writeMessageToFile(message, error) { + Logging.queue.push({ message: message, error: error }); + Logging.checkQueue(); + } + static checkQueue() { + if (Logging.writing) + return; + if (Logging.queue.length <= 0) + return; + Logging.writing = true; + var message = Logging.queue[0]; + Logging.fileStream.write(message.message + "\n", () => { + if (message.error) { + Logging.errorStream.write(message.message + "\n", () => { + Logging.queue.splice(Logging.queue.indexOf(message), 1); + Logging.writing = false; + Logging.checkQueue(); + }); + } + else { + Logging.queue.splice(Logging.queue.indexOf(message), 1); + Logging.writing = false; + Logging.checkQueue(); + } + }); + } + static async initializeFile() { + if (this.fileStream && this.errorStream) + return; + if (!this.logFileLocation) + return; + try { + var exists = util.promisify(fs.exists); + await new Promise((resolve, reject) => { + fs.exists(this.logFileLocation, (exists) => { + if (!exists) { + fs.mkdir(this.logFileLocation, (err) => { + if (err) { + reject(err); + } + else { + resolve(); + } + }); + } + else + resolve(); + }); + }); + Logging.fileStream = fs.createWriteStream(this.logFileLocation + "all.log", { flags: "a" }); + Logging.fileStream.write("\n\n"); + Logging.errorStream = fs.createWriteStream(this.logFileLocation + "error.log", { flags: "a" }); + Logging.errorStream.write("\n\n"); + } + catch (e) { + console.log(e); + } + } +} +Logging.logFileLocation = "./logs/"; +Logging.stdout = true; +Logging.writing = false; +Logging.queue = new Array(); +exports.Logging = Logging; +function _getCallerFile() { + try { + var err = new Error(); + var callerfile; + var currentfile; + Error.prepareStackTrace = function (err, stack) { return stack; }; + currentfile = err.stack.shift().getFileName(); + while (err.stack.length) { + callerfile = err.stack.shift().getFileName(); + if (currentfile !== callerfile) + return callerfile; + } + } + catch (err) { } + return undefined; +} +var LoggingTypes; +(function (LoggingTypes) { + LoggingTypes[LoggingTypes["Log"] = 0] = "Log"; + LoggingTypes[LoggingTypes["Warning"] = 1] = "Warning"; + LoggingTypes[LoggingTypes["Error"] = 2] = "Error"; + LoggingTypes[LoggingTypes["Debug"] = 3] = "Debug"; +})(LoggingTypes = exports.LoggingTypes || (exports.LoggingTypes = {})); +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/out/index.js.map b/out/index.js.map new file mode 100644 index 0000000..41e6ef7 --- /dev/null +++ b/out/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,6BAA6B;AAC7B,yBAAyB;AAEzB,MAAM,KAAK,GAAG,SAAS,CAAA;AACvB,MAAM,MAAM,GAAG,SAAS,CAAA;AACxB,MAAM,GAAG,GAAG,SAAS,CAAA;AACrB,MAAM,UAAU,GAAG,SAAS,CAAA;AAC5B,MAAM,KAAK,GAAG,SAAS,CAAA;AACvB,MAAM,OAAO,GAAG,SAAS,CAAA;AACzB,MAAM,MAAM,GAAG,SAAS,CAAA;AAExB,MAAM,OAAO,GAAG,UAAU,CAAA;AAC1B,MAAM,KAAK,GAAG,UAAU,CAAA;AACxB,MAAM,OAAO,GAAG,UAAU,CAAA;AAC1B,MAAM,QAAQ,GAAG,UAAU,CAAA;AAC3B,MAAM,MAAM,GAAG,UAAU,CAAA;AACzB,MAAM,SAAS,GAAG,UAAU,CAAA;AAC5B,MAAM,MAAM,GAAG,UAAU,CAAA;AACzB,MAAM,OAAO,GAAG,UAAU,CAAA;AAE1B,MAAM,OAAO,GAAG,UAAU,CAAA;AAC1B,MAAM,KAAK,GAAG,UAAU,CAAA;AACxB,MAAM,OAAO,GAAG,UAAU,CAAA;AAC1B,MAAM,QAAQ,GAAG,UAAU,CAAA;AAC3B,MAAM,MAAM,GAAG,UAAU,CAAA;AACzB,MAAM,SAAS,GAAG,UAAU,CAAA;AAC5B,MAAM,MAAM,GAAG,UAAU,CAAA;AACzB,MAAM,OAAO,GAAG,UAAU,CAAA;AAE1B;IAUG,MAAM,CAAC,MAAM,CAAC,SAAiB,EAAE,MAAe;QAC7C,IAAI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IACrB,6CAAG,CAAA;IACH,qDAAO,CAAA;IACP,iDAAK,CAAA;IACL,iDAAK,CAAA;AACR,CAAC,EALW,YAAY,GAAZ,oBAAY,KAAZ,oBAAY,QAKvB"} \ No newline at end of file diff --git a/out/test.d.ts b/out/test.d.ts new file mode 100644 index 0000000..e69de29 diff --git a/out/test.js b/out/test.js new file mode 100644 index 0000000..6f1288c --- /dev/null +++ b/out/test.js @@ -0,0 +1,17 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const index_1 = require("./index"); +index_1.Logging.log("test"); +index_1.Logging.log("i", "am", { a: "an" }, 1000); +index_1.Logging.error(new Error("fehler 001")); +index_1.Logging.debug("Some Debug infos"); +index_1.Logging.errorMessage("i", "am", "an", "error"); +/*Logging.config("./logs/tests/test", true); + +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"); +*/ +//# sourceMappingURL=test.js.map \ No newline at end of file diff --git a/out/test.js.map b/out/test.js.map new file mode 100644 index 0000000..b1d04ca --- /dev/null +++ b/out/test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,mCAAkC;AAElC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AACnB,eAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;AAC1C,eAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;AACvC,eAAO,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAClC,eAAO,CAAC,YAAY,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAE/C;;;;;;;EAOE"} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..059bdaf --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "logger", + "version": "1.0.0", + "description": "", + "main": "out/index.js", + "scripts": { + "build": "npm tsc", + "test": "node out/test.js" + }, + "repository": { + "type": "git", + "url": "https://git.stamm.me/PerfCloud/nodejslogger.git" + }, + "author": "Fabian Stamm", + "license": "MIT", + "devDependencies": { + "@types/node": "^8.0.24", + "typescript": "^2.4.2" + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..67a8f18 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,188 @@ +import * as util from "util"; +import * as fs from "fs"; + +const Reset = "\x1b[0m" +const Bright = "\x1b[1m" +const Dim = "\x1b[2m" +const Underscore = "\x1b[4m" +const Blink = "\x1b[5m" +const Reverse = "\x1b[7m" +const Hidden = "\x1b[8m" + +const FgBlack = "\x1b[30m" +const FgRed = "\x1b[31m" +const FgGreen = "\x1b[32m" +const FgYellow = "\x1b[33m" +const FgBlue = "\x1b[34m" +const FgMagenta = "\x1b[35m" +const FgCyan = "\x1b[36m" +const FgWhite = "\x1b[37m" + +const BgBlack = "\x1b[40m" +const BgRed = "\x1b[41m" +const BgGreen = "\x1b[42m" +const BgYellow = "\x1b[43m" +const BgBlue = "\x1b[44m" +const BgMagenta = "\x1b[45m" +const BgCyan = "\x1b[46m" +const BgWhite = "\x1b[47m" + +export class Logging { + private static logFileLocation: string = "./logs/"; + private static stdout: boolean = true; + + private static fileStream: fs.WriteStream; + private static errorStream: fs.WriteStream; + + private static writing = false; + private static queue = new Array<{ message: string, error: boolean }>(); + + static config(logfolder: string, stdout: boolean) { + this.logFileLocation = logfolder; + this.stdout = stdout; + } + + static debug(...message: any[]) { + Logging.message(LoggingTypes.Debug, message); + } + + static log(...message: any[]) { + Logging.message(LoggingTypes.Log, message); + } + + static warning(...message: any[]) { + Logging.message(LoggingTypes.Log, message); + } + + static error(error: Error | string) { + if (typeof error === "string") { + Logging.errorMessage(error); + return; + } + var message = error.name + " " + error.message + "\n" + error.stack; + Logging.message(LoggingTypes.Error, [message]); + } + + static errorMessage(...message: string[]) { + Logging.message(LoggingTypes.Error, message); + } + + private static async message(type: LoggingTypes, message: any[] | string) { + var consoleLogFormat = Reset; + switch (type) { + case LoggingTypes.Log: + //m += FgWhite + BgBlack; + break; + case LoggingTypes.Error: + consoleLogFormat += FgRed;//FgWhite + BgRed + FgWhite; + break; + case LoggingTypes.Debug: + consoleLogFormat += FgCyan; + break; + case LoggingTypes.Warning: + consoleLogFormat += FgYellow; + break; + } + var mb = ""; + if (typeof message === "string") { + mb = message; + } else { + message.forEach(e => { + if (typeof e !== "string") e = util.inspect(e, false, null); + if (e.endsWith("\n")) { + mb += e; + } else { + mb += e + " "; + } + }); + } + + var m = "[" + LoggingTypes[type] + "][" + _getCallerFile() + "][" + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') + "]: " + mb; + if (this.logFileLocation) { + if (!this.fileStream || !this.errorStream) { + await Logging.initializeFile(); + } + Logging.writeMessageToFile(m, type === LoggingTypes.Error); + } + if (this.stdout) console.log(consoleLogFormat + m + Reset); + } + + private static writeMessageToFile(message: string, error?: boolean) { + Logging.queue.push({ message: message, error: error }); + Logging.checkQueue(); + } + + private static checkQueue() { + if (Logging.writing) return; + if (Logging.queue.length <= 0) return; + Logging.writing = true; + var message = Logging.queue[0]; + Logging.fileStream.write(message.message + "\n", () => { + if (message.error) { + Logging.errorStream.write(message.message + "\n", () => { + Logging.queue.splice(Logging.queue.indexOf(message), 1); + Logging.writing = false; + Logging.checkQueue(); + }); + } else { + Logging.queue.splice(Logging.queue.indexOf(message), 1); + Logging.writing = false; + Logging.checkQueue(); + } + }); + } + + private static async initializeFile() { + if (this.fileStream && this.errorStream) return; + if (!this.logFileLocation) return; + try { + var exists = util.promisify(fs.exists); + await new Promise((resolve, reject) => { + fs.exists(this.logFileLocation, (exists) => { + if (!exists) { + fs.mkdir(this.logFileLocation, (err) => { + if (err) { + reject(err); + } else { + resolve(); + } + }); + } else resolve(); + }); + }); + + Logging.fileStream = fs.createWriteStream(this.logFileLocation + "all.log", { flags: "a" }); + Logging.fileStream.write("\n\n"); + Logging.errorStream = fs.createWriteStream(this.logFileLocation + "error.log", { flags: "a" }); + Logging.errorStream.write("\n\n"); + } catch (e) { + console.log(e); + } + } +} + +function _getCallerFile() { + try { + var err = new Error(); + var callerfile; + var currentfile; + + (Error).prepareStackTrace = function (err, stack) { return stack; }; + + currentfile = (err.stack).shift().getFileName(); + + while (err.stack.length) { + callerfile = (err.stack).shift().getFileName(); + + if (currentfile !== callerfile) return callerfile; + } + } catch (err) { } + return undefined; +} + +export enum LoggingTypes { + Log, + Warning, + Error, + Debug +} \ No newline at end of file diff --git a/src/test.ts b/src/test.ts new file mode 100644 index 0000000..a6a9d94 --- /dev/null +++ b/src/test.ts @@ -0,0 +1,7 @@ +import { Logging } from "./index"; + +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"); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..07ceece --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es2017", + "noImplicitAny": false, + "sourceMap": true, + "outDir": "out", + "declaration": true, + "typeRoots": [ + "node_modules/@types" + ] + }, + "exclude": [ + "node_modules" + ], + "include": [ + "src" + ] +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..af2d20b --- /dev/null +++ b/yarn.lock @@ -0,0 +1,11 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@types/node@^8.0.24": + version "8.0.24" + resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.24.tgz#06c580084d9add1fb40c1510ef0b448961246fb1" + +typescript@^2.4.2: + version "2.4.2" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.4.2.tgz#f8395f85d459276067c988aa41837a8f82870844"