1
0
mirror of https://git.stamm.me/OpenServer/NodeLogging.git synced 2024-11-21 17:19:23 +00:00

First Commit

This commit is contained in:
Fabian Stamm 2017-08-23 16:45:03 +02:00
commit 8e7c381863
12 changed files with 472 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules/
logs/

24
out/index.d.ts vendored Normal file
View File

@ -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,
}

182
out/index.js Normal file
View File

@ -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

1
out/index.js.map Normal file

File diff suppressed because one or more lines are too long

0
out/test.d.ts vendored Normal file
View File

17
out/test.js Normal file
View File

@ -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

1
out/test.js.map Normal file
View File

@ -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"}

20
package.json Normal file
View File

@ -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"
}
}

188
src/index.ts Normal file
View File

@ -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;
(<any>Error).prepareStackTrace = function (err, stack) { return stack; };
currentfile = (<any>err.stack).shift().getFileName();
while (err.stack.length) {
callerfile = (<any>err.stack).shift().getFileName();
if (currentfile !== callerfile) return callerfile;
}
} catch (err) { }
return undefined;
}
export enum LoggingTypes {
Log,
Warning,
Error,
Debug
}

7
src/test.ts Normal file
View File

@ -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");

19
tsconfig.json Normal file
View File

@ -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"
]
}

11
yarn.lock Normal file
View File

@ -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"