From e36f735011ddf235aec11141effd5e6be909567f Mon Sep 17 00:00:00 2001 From: Julian Payne Date: Sun, 19 Apr 2020 04:13:23 +0900 Subject: [PATCH 1/5] initial change commit --- .gitignore | 1 + index.js | 28 ++++++ index.ts | 39 ++++++++ logger.js | 2 +- logger.ts | 243 ++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 20 ++++ package.json | 19 ++-- test.ts | 2 + 8 files changed, 347 insertions(+), 7 deletions(-) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 index.ts create mode 100644 logger.ts create mode 100644 package-lock.json create mode 100644 test.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/index.js b/index.js new file mode 100644 index 0000000..90b12d8 --- /dev/null +++ b/index.js @@ -0,0 +1,28 @@ +function test(obj) { + obj.write("success\n"); + console.log("write" in obj); +} +// test(process.stdout); +var TestClass = /** @class */ (function () { + function TestClass(arg1) { + if (arg1) { + console.log(arg1); + this.construct = arg1; + } + console.dir(this); + } + TestClass.prototype.outside = function (arg1) { + if (arg1) + console.log(arg1); + console.dir(this); + }; + return TestClass; +}()); +TestClass.prototype.proto = function (arg1) { + console.dir(this); + console.dir(arg1); + return "this is a prototype"; +}; +var tscls = new TestClass("constructor"); +tscls.outside("testing class method"); +TestClass.prototype.proto("this is a proto"); diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..1045f86 --- /dev/null +++ b/index.ts @@ -0,0 +1,39 @@ +interface WritableObject { + write(text: string): any; +} + +function test(obj: WritableObject): void { + obj.write("success\n"); + console.log("write" in obj); +} + +// test(process.stdout); + +class TestClass { + construct: any; + proto: any; + constructor(arg1?: any) { + if (arg1) { + console.log(arg1); + this.construct = arg1; + } + console.dir(this); + + } + + outside(arg1?: any) { + if (arg1) console.log(arg1); + console.dir(this); + + } +} + +TestClass.prototype.proto = function(arg1: any): string { + console.dir(this); + console.dir(arg1); + return "this is a prototype"; +} + +const tscls = new TestClass("constructor"); +tscls.outside("testing class method"); +TestClass.prototype.proto("this is a proto"); diff --git a/logger.js b/logger.js index 33e5690..615a3da 100644 --- a/logger.js +++ b/logger.js @@ -112,4 +112,4 @@ Logger.levels.forEach(function(level) { exports.Logger = Logger; exports.createLogger = function(log_file_path) { return new Logger(log_file_path); -}; \ No newline at end of file +}; diff --git a/logger.ts b/logger.ts new file mode 100644 index 0000000..30d9154 --- /dev/null +++ b/logger.ts @@ -0,0 +1,243 @@ +// name: logger.ts +// version: 0.0.2 +// http://github.com/quirkey/node-logger +/* + +Copyright (c) 2010 Aaron Quint + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +*/ + +/** + * @deprecated module sys: "sys.print" is replaced with process.stdout. + * : // sys = require("sys"); + */ +const path = require('path'); +const util = require("util"); +const fs = require('fs'); + + +function makeArray(nonarray: any): Array { + return Array.prototype.slice.call(nonarray); +}; + +// Create a new instance of Logger, logging to the file at `log_file_path` +// if `log_file_path` is null, log to STDOUT. + +interface WritableLogStream { + write(text: string): any; +} + +/** + * + */ +class Logger { + + STDOUT = process.stdout; + STDERR = process.stderr; + DEFAULT_LEVEL = 3; + levels = ["fatal", "error", "warn", "info", "debug"]; + stream: WritableLogStream; + fatal: object; + error: object; + warn: object; + info: object; + error: object; + + /** + * Instantiates the Logger class and configures "stream" and "log_level_index" properties. + * + * @param {string|object|number} log_file_path: Path to writeable stream. + * : You can pass an object that implements "write" either directly or in it's prototype. + * : The log will than be call the write function of that object with the specified format. + * + * @param {number} log_level_index: Optional. Sets the "log_level_index" property. + * + * @default {object} stream: process.env.stdout + * @default {number} log_level_index: 3 // which is "info". + */ + constructor(log_file_path?: any, log_level_index?: number) { + if (log_file_path === ("STDOUT" || 1)) { + this.stream = this.STDOUT; + } + else if (log_file_path === ("STDERR" || 2)) { + this.stream = this.STDERR; + } + else if (typeof log_file_path === "string") { + this.stream = fs.createWriteStream(log_file_path, {flags: "a", encoding: "utf8", mode: 0666}); + } + else if ((typeof log_file_path === "object") && ("write" in log_file_path)) { + this.stream = log_file_path; + this.stream.write("\n"); + } + else { + this.stream = this.STDOUT; + } + this.log_level_index = log_level_index || this.DEFAULT_LEVEL; + this.levels.forEach(level => { + this[level] = function() { + let args = makeArray(arguments); + args.unshift(level); + return this.log.apply(this, args); + } + }); + } + + /** + * The stream used in this function is defined in the constructor. + * : You can pass an object that implements "write" or + * : in it's prototype. + * + * @param {string} text: the text to write to the stream. + */ + protected write(text): void { + this.stream.write(text); + } + + /** + * The default log formatting function. + * @param {int} level: a number between 1 ~ 5. + * @param {string} date: Default format is "Sat Jun 2010 01:12:05 GMT-0400 (EDT)". + * @param {string} message: The message defined by the caller. + * @return {string} output: log message. + * + * The default format looks like: + * - "error [Sat Jun 12 2010 01:12:05 GMT-0400 (EDT)] message ..." + */ + public format(level: string, date: Date, message: string): string { + return [level, ' [', date, '] ', message].join(''); + } + + /** + * Sets the maximum log level. The default level is "info" or 4. + * @param {int} new_level: a number between 1 ~ 5. + * @return {boolean} success: returns true when the new_level is successfully set, otherwise false. + */ + public setLevel(new_level: string | number): number | boolean { + if ((typeof new_level === "number") && (this.levels[new_level - 1] !== undefined)) { + this.log_level_index = new_level - 1; + return this.log_level_index; + } + if ((typeof new_level === "string") && (this.levels.indexOf(new_level.toLowerCase()) !== -1)) { + this.log_level_index = this.levels.indexOf(new_level.toLowerCase()); + return this.log_level_index; + } + else { + return false; + } + } + + /** + * + */ + public log(): string | boolean { + let args = makeArray(arguments); + let message = ""; + let log_index = (this.levels.indexOf(args[0]) !== -1) ? args[0] : this.log_index_level; + if (log_index > this.log_level_index) { + return false; + } else { + args.forEach(arg => { + if (typeof arg === "string") { + message += " " + arg; + } else { + message += " " + util.inspect(arg, false, null); + } + }); + message = this.format(this.levels[log_index], new Date(), message); + this.write(message + "\n"); + return message; + } + } +} + +const DefaultLogger = Logger(); + +/** + * The default log formatting function. + * @param {int} level: a number between 1 ~ 5. + * @param {string} date: Default format is "Sat Jun 2010 01:12:05 GMT-0400 (EDT)". + * @param {string} message: The message defined by the caller. + * @return {string} output: log message. + * + * The default format looks like: + * - "error [Sat Jun 12 2010 01:12:05 GMT-0400 (EDT)] message ..." + */ +Logger.prototype.format = function(level, date, message) { + return [level, ' [', date, '] ', message].join(''); +}; + +/** + * Sets the maximum log level. The default level is "info" or 3. + * @param {int} new_level: a number between 1 ~ 5. + * @return {boolean} success: returns true when the new_level is successfully set, otherwise false. + */ +Logger.prototype.setLevel = function(new_level) { + var index = Logger.levels.indexOf(new_level); + return (index != -1) ? this.log_level_index = index : false; +}; + +// The base logging method. If the first argument is one of the levels, it logs +// to that level, otherwise, logs to the default level. Can take `n` arguments +// and joins them by ' '. If the argument is not a string, it runs `sys.inspect()` +// to print a string representation of the object. +Logger.prototype.log = function() { + var args = makeArray(arguments), + log_index = Logger.levels.indexOf(args[0]), + message = ''; + + // if you're just default logging + if (log_index === -1) { + log_index = this.log_level_index; + } else { + // the first arguement actually was the log level + args.shift(); + } + if (log_index <= this.log_level_index) { + // join the arguments into a loggable string + args.forEach(function(arg) { + if (typeof arg === 'string') { + message += ' ' + arg; + } else { + message += ' ' + sys.inspect(arg, false, null); + } + }); + message = this.format(Logger.levels[log_index], new Date(), message); + this.write(message + "\n"); + return message; + } + return false; +}; + +Logger.levels.forEach(function(level) { + Logger.prototype[level] = function() { + var args = makeArray(arguments); + args.unshift(level); + return this.log.apply(this, args); + }; +}); + +exports.Logger = Logger; +exports.createLogger = function(log_file_path) { + return new Logger(log_file_path); +}; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..67a9c72 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,20 @@ +{ + "name": "logger", + "version": "0.0.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@types/node": { + "version": "13.13.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.0.tgz", + "integrity": "sha512-WE4IOAC6r/yBZss1oQGM5zs2D7RuKR6Q+w+X2SouPofnWn+LbCqClRyhO3ZE7Ix8nmFgo/oVuuE01cJT2XB13A==", + "dev": true + }, + "typescript": { + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.3.tgz", + "integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==", + "dev": true + } + } +} diff --git a/package.json b/package.json index 9bc52c1..b7c09b8 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,14 @@ -{ "name" : "logger" -, "version" : "0.0.1" -, "description" : "A simple logging library that combines the simple APIs of Ruby's logger.rb and browser-js console.log()" -, "author" : "Aaron Quint " -, "main" : "logger" -, "engines" : { "node" : ">=0.1.90" } +{ + "name": "logger", + "version": "0.0.1", + "description": "A simple logging library that combines the simple APIs of Ruby's logger.rb and browser-js console.log()", + "author": "Aaron Quint ", + "main": "logger", + "engines": { + "node": ">=0.1.90" + }, + "devDependencies": { + "@types/node": "^13.13.0", + "typescript": "^3.8.3" + } } diff --git a/test.ts b/test.ts new file mode 100644 index 0000000..b443e14 --- /dev/null +++ b/test.ts @@ -0,0 +1,2 @@ +import { strict as assert } from 'assert'; + From 9e48634648e7612074a33db5fd44752772aab041 Mon Sep 17 00:00:00 2001 From: Julian Payne Date: Sun, 19 Apr 2020 17:29:14 +0900 Subject: [PATCH 2/5] finallizing ... --- .gitignore | 3 + index.js | 28 ------ index.ts | 39 -------- logger.js | 232 +++++++++++++++++++++++++++++----------------- logger.js.old | 115 +++++++++++++++++++++++ logger.ts | 151 ++++++++++++------------------ package-lock.json | 20 ---- test.ts | 2 - 8 files changed, 321 insertions(+), 269 deletions(-) delete mode 100644 index.js delete mode 100644 index.ts create mode 100644 logger.js.old delete mode 100644 package-lock.json delete mode 100644 test.ts diff --git a/.gitignore b/.gitignore index 3c3629e..977bbc3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ node_modules +test.[t|j]s +package-lock.json + diff --git a/index.js b/index.js deleted file mode 100644 index 90b12d8..0000000 --- a/index.js +++ /dev/null @@ -1,28 +0,0 @@ -function test(obj) { - obj.write("success\n"); - console.log("write" in obj); -} -// test(process.stdout); -var TestClass = /** @class */ (function () { - function TestClass(arg1) { - if (arg1) { - console.log(arg1); - this.construct = arg1; - } - console.dir(this); - } - TestClass.prototype.outside = function (arg1) { - if (arg1) - console.log(arg1); - console.dir(this); - }; - return TestClass; -}()); -TestClass.prototype.proto = function (arg1) { - console.dir(this); - console.dir(arg1); - return "this is a prototype"; -}; -var tscls = new TestClass("constructor"); -tscls.outside("testing class method"); -TestClass.prototype.proto("this is a proto"); diff --git a/index.ts b/index.ts deleted file mode 100644 index 1045f86..0000000 --- a/index.ts +++ /dev/null @@ -1,39 +0,0 @@ -interface WritableObject { - write(text: string): any; -} - -function test(obj: WritableObject): void { - obj.write("success\n"); - console.log("write" in obj); -} - -// test(process.stdout); - -class TestClass { - construct: any; - proto: any; - constructor(arg1?: any) { - if (arg1) { - console.log(arg1); - this.construct = arg1; - } - console.dir(this); - - } - - outside(arg1?: any) { - if (arg1) console.log(arg1); - console.dir(this); - - } -} - -TestClass.prototype.proto = function(arg1: any): string { - console.dir(this); - console.dir(arg1); - return "this is a prototype"; -} - -const tscls = new TestClass("constructor"); -tscls.outside("testing class method"); -TestClass.prototype.proto("this is a proto"); diff --git a/logger.js b/logger.js index 615a3da..db8cf04 100644 --- a/logger.js +++ b/logger.js @@ -1,5 +1,5 @@ -// name: logger.js -// version: 0.0.1 +// name: logger.ts +// version: 0.0.2 // http://github.com/quirkey/node-logger /* @@ -27,89 +27,149 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - -var path = require('path'), - sys = require('sys'), - fs = require('fs'); - -var makeArray = function(nonarray) { - return Array.prototype.slice.call(nonarray); -}; - -// Create a new instance of Logger, logging to the file at `log_file_path` -// if `log_file_path` is null, log to STDOUT. -var Logger = function(log_file_path) { - // default write is STDOUT - this.write = sys.print; - this.log_level_index = 3; - - // if a path is given, try to write to it - if (log_file_path) { - // Write to a file - log_file_path = path.normalize(log_file_path); - this.stream = fs.createWriteStream(log_file_path, {flags: 'a', encoding: 'utf8', mode: 0666}); - this.stream.write("\n"); - this.write = function(text) { this.stream.write(text); }; - } -}; - -Logger.levels = ['fatal', 'error', 'warn', 'info', 'debug']; - -// The default log formatting function. The default format looks something like: -// -// error [Sat Jun 12 2010 01:12:05 GMT-0400 (EDT)] message -// -Logger.prototype.format = function(level, date, message) { - return [level, ' [', date, '] ', message].join(''); -}; - -// Set the maximum log level. The default level is "info". -Logger.prototype.setLevel = function(new_level) { - var index = Logger.levels.indexOf(new_level); - return (index != -1) ? this.log_level_index = index : false; -}; - -// The base logging method. If the first argument is one of the levels, it logs -// to that level, otherwise, logs to the default level. Can take `n` arguments -// and joins them by ' '. If the argument is not a string, it runs `sys.inspect()` -// to print a string representation of the object. -Logger.prototype.log = function() { - var args = makeArray(arguments), - log_index = Logger.levels.indexOf(args[0]), - message = ''; - - // if you're just default logging - if (log_index === -1) { - log_index = this.log_level_index; - } else { - // the first arguement actually was the log level - args.shift(); - } - if (log_index <= this.log_level_index) { - // join the arguments into a loggable string - args.forEach(function(arg) { - if (typeof arg === 'string') { - message += ' ' + arg; - } else { - message += ' ' + sys.inspect(arg, false, null); - } - }); - message = this.format(Logger.levels[log_index], new Date(), message); - this.write(message + "\n"); - return message; - } - return false; -}; - -Logger.levels.forEach(function(level) { - Logger.prototype[level] = function() { - var args = makeArray(arguments); - args.unshift(level); - return this.log.apply(this, args); - }; -}); - +/** + * @deprecated module sys: "sys.print" is replaced with process.stdout. + * : // sys = require("sys"); + */ +var path = require('path'); +var util = require("util"); +var fs = require('fs'); +function makeArray(nonarray) { + return Array.prototype.slice.call(nonarray); +} +; +/** + * + */ +var Logger = /** @class */ (function () { + /** + * Instantiates the Logger class and configures "stream" and "log_level_index" properties. + * + * @param {string|object|number} log_file_path: Path to writeable stream. + * : You can pass an object that implements "write" either directly or in it's prototype. + * : The log will than be call the write function of that object with the specified format. + * + * @param {number} log_level_index: Optional. Sets the "log_level_index" property. + * + * @default {object} stream: process.env.stdout + * @default {number} log_level_index: 3 // which is "info". + */ + function Logger(log_file_path, log_level) { + this.levels = ["fatal", "error", "warn", "info", "debug"]; + this.log_level_default = 3; + if (log_file_path === 1 || log_file_path === "STDOUT") { + this.stream = process.stdout; + } + else if (log_file_path === 2 || log_file_path === "STDERR") { + this.stream = process.stderr; + } + else if (typeof log_file_path === "string") { + this.stream = fs.createWriteStream(log_file_path, { flags: "a", encoding: "utf8", mode: parseInt("0666", 10) }); + } + else if ((typeof log_file_path === "object") && ("write" in log_file_path)) { + this.stream = log_file_path; + this.stream.write("\n"); + } + else { + this.stream = process.stdout; + } + this.log_level_index = typeof log_level === "string" ? this.levels.indexOf(log_level) : log_level || this.log_level_default; + } + /** + * The stream used in this function is defined in the constructor. + * : You can pass an object that implements "write" or + * : in it's prototype. + * + * @param {string} text: the text to write to the stream. + */ + Logger.prototype.write = function (text) { + this.stream.write(text); + }; + /** + * The default log formatting function. + * @param {int} level: a number between 1 ~ 5. + * @param {string} date: Default format is "Sat Jun 2010 01:12:05 GMT-0400 (EDT)". + * @param {string} message: The message defined by the caller. + * @return {string} output: log message. + * + * The default format looks like: + * - "error [Sat Jun 12 2010 01:12:05 GMT-0400 (EDT)] message ..." + */ + Logger.prototype.format = function (level, date, message) { + return [level, ' [', date, '] ', message].join(''); + }; + /** + * Sets the maximum log level. The default level is "info" or 4. + * @param {int} new_level: a number between 1 ~ 5. + * @return {boolean} success: returns true when the new_level is successfully set, otherwise false. + */ + Logger.prototype.setLevel = function (new_level) { + if ((typeof new_level === "number") && (this.levels[new_level - 1] !== undefined)) { + this.log_level_index = new_level - 1; + return this.log_level_index; + } + if ((typeof new_level === "string") && (this.levels.indexOf(new_level.toLowerCase()) !== -1)) { + this.log_level_index = this.levels.indexOf(new_level.toLowerCase()); + return this.log_level_index; + } + else { + return false; + } + }; + /** + * Calls "this.stream.write()" with newline appended. + * @return {string} message: log message with newline appended. + */ + Logger.prototype.log = function () { + var args = makeArray(arguments); + var message = ""; + var log_index = (this.levels.indexOf(args[0].toLowerCase()) !== -1) ? this.levels.indexOf(args[0]) : this.log_level_index; + if (log_index > this.log_level_index) { + return false; + } + else { + args.shift(); + args.forEach(function (arg) { + if (typeof arg === "string") { + message += " " + arg; + } + else { + message += " " + util.inspect(arg, false, null); + } + }); + message = this.format(this.levels[log_index], new Date(), message); + this.write(message + "\n"); + return message; + } + }; + Logger.prototype.fatal = function () { + var args = makeArray(arguments); + args.unshift("fatal"); + return this.log.apply(this, args); + }; + Logger.prototype.error = function () { + var args = makeArray(arguments); + args.unshift("errors"); + return this.log.apply(this, args); + }; + Logger.prototype.warn = function () { + var args = makeArray(arguments); + args.unshift("warn"); + return this.log.apply(this, args); + }; + Logger.prototype.info = function () { + var args = makeArray(arguments); + args.unshift("info"); + return this.log.apply(this, args); + }; + Logger.prototype.debug = function () { + var args = makeArray(arguments); + args.unshift("debug"); + return this.log.apply(this, args); + }; + return Logger; +}()); exports.Logger = Logger; -exports.createLogger = function(log_file_path) { - return new Logger(log_file_path); +exports.createLogger = function (log_file_path) { + return new Logger(log_file_path); }; diff --git a/logger.js.old b/logger.js.old new file mode 100644 index 0000000..615a3da --- /dev/null +++ b/logger.js.old @@ -0,0 +1,115 @@ +// name: logger.js +// version: 0.0.1 +// http://github.com/quirkey/node-logger +/* + +Copyright (c) 2010 Aaron Quint + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +*/ + +var path = require('path'), + sys = require('sys'), + fs = require('fs'); + +var makeArray = function(nonarray) { + return Array.prototype.slice.call(nonarray); +}; + +// Create a new instance of Logger, logging to the file at `log_file_path` +// if `log_file_path` is null, log to STDOUT. +var Logger = function(log_file_path) { + // default write is STDOUT + this.write = sys.print; + this.log_level_index = 3; + + // if a path is given, try to write to it + if (log_file_path) { + // Write to a file + log_file_path = path.normalize(log_file_path); + this.stream = fs.createWriteStream(log_file_path, {flags: 'a', encoding: 'utf8', mode: 0666}); + this.stream.write("\n"); + this.write = function(text) { this.stream.write(text); }; + } +}; + +Logger.levels = ['fatal', 'error', 'warn', 'info', 'debug']; + +// The default log formatting function. The default format looks something like: +// +// error [Sat Jun 12 2010 01:12:05 GMT-0400 (EDT)] message +// +Logger.prototype.format = function(level, date, message) { + return [level, ' [', date, '] ', message].join(''); +}; + +// Set the maximum log level. The default level is "info". +Logger.prototype.setLevel = function(new_level) { + var index = Logger.levels.indexOf(new_level); + return (index != -1) ? this.log_level_index = index : false; +}; + +// The base logging method. If the first argument is one of the levels, it logs +// to that level, otherwise, logs to the default level. Can take `n` arguments +// and joins them by ' '. If the argument is not a string, it runs `sys.inspect()` +// to print a string representation of the object. +Logger.prototype.log = function() { + var args = makeArray(arguments), + log_index = Logger.levels.indexOf(args[0]), + message = ''; + + // if you're just default logging + if (log_index === -1) { + log_index = this.log_level_index; + } else { + // the first arguement actually was the log level + args.shift(); + } + if (log_index <= this.log_level_index) { + // join the arguments into a loggable string + args.forEach(function(arg) { + if (typeof arg === 'string') { + message += ' ' + arg; + } else { + message += ' ' + sys.inspect(arg, false, null); + } + }); + message = this.format(Logger.levels[log_index], new Date(), message); + this.write(message + "\n"); + return message; + } + return false; +}; + +Logger.levels.forEach(function(level) { + Logger.prototype[level] = function() { + var args = makeArray(arguments); + args.unshift(level); + return this.log.apply(this, args); + }; +}); + +exports.Logger = Logger; +exports.createLogger = function(log_file_path) { + return new Logger(log_file_path); +}; diff --git a/logger.ts b/logger.ts index 30d9154..c4ed9ac 100644 --- a/logger.ts +++ b/logger.ts @@ -37,32 +37,35 @@ const util = require("util"); const fs = require('fs'); -function makeArray(nonarray: any): Array { +function makeArray(nonarray: any): Array { return Array.prototype.slice.call(nonarray); }; -// Create a new instance of Logger, logging to the file at `log_file_path` -// if `log_file_path` is null, log to STDOUT. - +/** + * Interface for "Logger.stream" object. Called by "Logger.prototype.log()". + * : Logger.stream can be set to anything that has implements "write" in it's object or prototype. + * + * @param {string} text: String to write to "stdout | stderr | logfile". + */ interface WritableLogStream { write(text: string): any; } +/** + * For reference in Logger constructor. + */ +type FileDescriptor = 1 | 2; +type LogLevels = "fatal" | "error" | "warn" | "info" | "debug" | 1 | 2 | 3 | 4 | 5; + /** * */ class Logger { - STDOUT = process.stdout; - STDERR = process.stderr; - DEFAULT_LEVEL = 3; levels = ["fatal", "error", "warn", "info", "debug"]; stream: WritableLogStream; - fatal: object; - error: object; - warn: object; - info: object; - error: object; + log_level_index: number; + log_level_default = 3; /** * Instantiates the Logger class and configures "stream" and "log_level_index" properties. @@ -76,31 +79,24 @@ class Logger { * @default {object} stream: process.env.stdout * @default {number} log_level_index: 3 // which is "info". */ - constructor(log_file_path?: any, log_level_index?: number) { - if (log_file_path === ("STDOUT" || 1)) { - this.stream = this.STDOUT; + constructor(log_file_path?: string | FileDescriptor | WritableLogStream, log_level?: LogLevels) { + if (log_file_path === 1 || log_file_path === "STDOUT") { + this.stream = process.stdout; } - else if (log_file_path === ("STDERR" || 2)) { - this.stream = this.STDERR; + else if (log_file_path === 2 || log_file_path === "STDERR") { + this.stream = process.stderr; } else if (typeof log_file_path === "string") { - this.stream = fs.createWriteStream(log_file_path, {flags: "a", encoding: "utf8", mode: 0666}); + this.stream = fs.createWriteStream(log_file_path, {flags: "a", encoding: "utf8", mode: parseInt("0666", 10)}); } else if ((typeof log_file_path === "object") && ("write" in log_file_path)) { this.stream = log_file_path; this.stream.write("\n"); } else { - this.stream = this.STDOUT; + this.stream = process.stdout; } - this.log_level_index = log_level_index || this.DEFAULT_LEVEL; - this.levels.forEach(level => { - this[level] = function() { - let args = makeArray(arguments); - args.unshift(level); - return this.log.apply(this, args); - } - }); + this.log_level_index = typeof log_level === "string" ? this.levels.indexOf(log_level) : log_level || this.log_level_default; } /** @@ -148,15 +144,17 @@ class Logger { } /** - * + * Calls "this.stream.write()" with newline appended. + * @return {string} message: log message with newline appended. */ public log(): string | boolean { let args = makeArray(arguments); let message = ""; - let log_index = (this.levels.indexOf(args[0]) !== -1) ? args[0] : this.log_index_level; + let log_index = (this.levels.indexOf(args[0].toLowerCase()) !== -1) ? this.levels.indexOf(args[0]) : this.log_level_index; if (log_index > this.log_level_index) { return false; } else { + args.shift(); args.forEach(arg => { if (typeof arg === "string") { message += " " + arg; @@ -169,75 +167,40 @@ class Logger { return message; } } -} -const DefaultLogger = Logger(); - -/** - * The default log formatting function. - * @param {int} level: a number between 1 ~ 5. - * @param {string} date: Default format is "Sat Jun 2010 01:12:05 GMT-0400 (EDT)". - * @param {string} message: The message defined by the caller. - * @return {string} output: log message. - * - * The default format looks like: - * - "error [Sat Jun 12 2010 01:12:05 GMT-0400 (EDT)] message ..." - */ -Logger.prototype.format = function(level, date, message) { - return [level, ' [', date, '] ', message].join(''); -}; - -/** - * Sets the maximum log level. The default level is "info" or 3. - * @param {int} new_level: a number between 1 ~ 5. - * @return {boolean} success: returns true when the new_level is successfully set, otherwise false. - */ -Logger.prototype.setLevel = function(new_level) { - var index = Logger.levels.indexOf(new_level); - return (index != -1) ? this.log_level_index = index : false; -}; + public fatal() { + let args = makeArray(arguments); + args.unshift("fatal"); + return this.log.apply(this, args); + } -// The base logging method. If the first argument is one of the levels, it logs -// to that level, otherwise, logs to the default level. Can take `n` arguments -// and joins them by ' '. If the argument is not a string, it runs `sys.inspect()` -// to print a string representation of the object. -Logger.prototype.log = function() { - var args = makeArray(arguments), - log_index = Logger.levels.indexOf(args[0]), - message = ''; - - // if you're just default logging - if (log_index === -1) { - log_index = this.log_level_index; - } else { - // the first arguement actually was the log level - args.shift(); - } - if (log_index <= this.log_level_index) { - // join the arguments into a loggable string - args.forEach(function(arg) { - if (typeof arg === 'string') { - message += ' ' + arg; - } else { - message += ' ' + sys.inspect(arg, false, null); - } - }); - message = this.format(Logger.levels[log_index], new Date(), message); - this.write(message + "\n"); - return message; - } - return false; -}; + public error() { + let args = makeArray(arguments); + args.unshift("errors"); + return this.log.apply(this, args); + } + + public warn() { + let args = makeArray(arguments); + args.unshift("warn"); + return this.log.apply(this, args); + } -Logger.levels.forEach(function(level) { - Logger.prototype[level] = function() { - var args = makeArray(arguments); - args.unshift(level); - return this.log.apply(this, args); - }; -}); + public info() { + let args = makeArray(arguments); + args.unshift("info"); + return this.log.apply(this, args); + } + + public debug() { + let args = makeArray(arguments); + args.unshift("debug"); + return this.log.apply(this, args); + } + +} exports.Logger = Logger; exports.createLogger = function(log_file_path) { - return new Logger(log_file_path); + return new Logger(log_file_path); }; diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 67a9c72..0000000 --- a/package-lock.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "name": "logger", - "version": "0.0.1", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "@types/node": { - "version": "13.13.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.0.tgz", - "integrity": "sha512-WE4IOAC6r/yBZss1oQGM5zs2D7RuKR6Q+w+X2SouPofnWn+LbCqClRyhO3ZE7Ix8nmFgo/oVuuE01cJT2XB13A==", - "dev": true - }, - "typescript": { - "version": "3.8.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.3.tgz", - "integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==", - "dev": true - } - } -} diff --git a/test.ts b/test.ts deleted file mode 100644 index b443e14..0000000 --- a/test.ts +++ /dev/null @@ -1,2 +0,0 @@ -import { strict as assert } from 'assert'; - From 952b1bc9272340a4524ea881cba59f1ee4387a69 Mon Sep 17 00:00:00 2001 From: Julian Payne Date: Wed, 29 Apr 2020 17:58:13 +0900 Subject: [PATCH 3/5] stable --- logger.js | 14 +++++++++++++- logger.ts | 22 ++++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/logger.js b/logger.js index db8cf04..cb97c58 100644 --- a/logger.js +++ b/logger.js @@ -55,6 +55,7 @@ var Logger = /** @class */ (function () { * @default {number} log_level_index: 3 // which is "info". */ function Logger(log_file_path, log_level) { + var _this = this; this.levels = ["fatal", "error", "warn", "info", "debug"]; this.log_level_default = 3; if (log_file_path === 1 || log_file_path === "STDOUT") { @@ -74,6 +75,17 @@ var Logger = /** @class */ (function () { this.stream = process.stdout; } this.log_level_index = typeof log_level === "string" ? this.levels.indexOf(log_level) : log_level || this.log_level_default; + // This is here for the puspose of assigning "Logger.levels" dynamically to ... + // this class' as prototype methods just like in the original source. + this.levels.forEach(function (level) { + if (_this[level] === undefined) { + Object.assign(_this, function () { + var args = makeArray(arguments); + args.unshift(level); + return this.log.apply(this, args); + }); + } + }); } /** * The stream used in this function is defined in the constructor. @@ -149,7 +161,7 @@ var Logger = /** @class */ (function () { }; Logger.prototype.error = function () { var args = makeArray(arguments); - args.unshift("errors"); + args.unshift("error"); return this.log.apply(this, args); }; Logger.prototype.warn = function () { diff --git a/logger.ts b/logger.ts index c4ed9ac..8aa36eb 100644 --- a/logger.ts +++ b/logger.ts @@ -51,6 +51,7 @@ interface WritableLogStream { write(text: string): any; } + /** * For reference in Logger constructor. */ @@ -97,6 +98,20 @@ class Logger { this.stream = process.stdout; } this.log_level_index = typeof log_level === "string" ? this.levels.indexOf(log_level) : log_level || this.log_level_default; + + // This is here for the puspose of assigning "Logger.levels" dynamically to ... + // this class' as a log call just like in the original source, except that ... + // It does not assign it to the Logger.prototype because it is in the constrcutor. + // I don't know how to implement this correctly so if anyone knows a better way, please do so. + this.levels.forEach(level => { + if (this[level] === undefined) { + Object.assign(this, function() { + let args = makeArray(arguments); + args.unshift(level); + return this.log.apply(this, args); + }) + } + }) } /** @@ -147,7 +162,7 @@ class Logger { * Calls "this.stream.write()" with newline appended. * @return {string} message: log message with newline appended. */ - public log(): string | boolean { + public log(): string | false { let args = makeArray(arguments); let message = ""; let log_index = (this.levels.indexOf(args[0].toLowerCase()) !== -1) ? this.levels.indexOf(args[0]) : this.log_level_index; @@ -176,7 +191,7 @@ class Logger { public error() { let args = makeArray(arguments); - args.unshift("errors"); + args.unshift("error"); return this.log.apply(this, args); } @@ -197,10 +212,9 @@ class Logger { args.unshift("debug"); return this.log.apply(this, args); } - } exports.Logger = Logger; -exports.createLogger = function(log_file_path) { +exports.createLogger = function(log_file_path: WritableLogStream | string): Logger { return new Logger(log_file_path); }; From 255536f203cbae035af042e2068d3fe8e9b27518 Mon Sep 17 00:00:00 2001 From: Julian Payne Date: Wed, 29 Apr 2020 18:05:12 +0900 Subject: [PATCH 4/5] added tsconfig.json --- logger.js | 4 +++- tsconfig.json | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tsconfig.json diff --git a/logger.js b/logger.js index cb97c58..45d2a07 100644 --- a/logger.js +++ b/logger.js @@ -76,7 +76,9 @@ var Logger = /** @class */ (function () { } this.log_level_index = typeof log_level === "string" ? this.levels.indexOf(log_level) : log_level || this.log_level_default; // This is here for the puspose of assigning "Logger.levels" dynamically to ... - // this class' as prototype methods just like in the original source. + // this class' as a log call just like in the original source, except that ... + // It does not assign it to the Logger.prototype because it is in the constrcutor. + // I don't know how to implement this correctly so if anyone knows a better way, please do so. this.levels.forEach(function (level) { if (_this[level] === undefined) { Object.assign(_this, function () { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..e71712d --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compileOnSave": true, + "complilerOptions": { + "module": "commonjs", + "noImplicitAny": true, + "sourceMap": true + }, + "files": [ + "logger.ts" + ] +} From d16739945c22e70f584ddf0c738e1e613f896208 Mon Sep 17 00:00:00 2001 From: Julian Payne Date: Wed, 29 Apr 2020 18:13:13 +0900 Subject: [PATCH 5/5] fixed createLogger method to also receive logLevel --- logger.js | 4 ++-- logger.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/logger.js b/logger.js index 45d2a07..43be367 100644 --- a/logger.js +++ b/logger.js @@ -184,6 +184,6 @@ var Logger = /** @class */ (function () { return Logger; }()); exports.Logger = Logger; -exports.createLogger = function (log_file_path) { - return new Logger(log_file_path); +exports.createLogger = function (log_file_path, logLevel) { + return new Logger(log_file_path, logLevel); }; diff --git a/logger.ts b/logger.ts index 8aa36eb..09f8245 100644 --- a/logger.ts +++ b/logger.ts @@ -215,6 +215,6 @@ class Logger { } exports.Logger = Logger; -exports.createLogger = function(log_file_path: WritableLogStream | string): Logger { - return new Logger(log_file_path); +exports.createLogger = function(log_file_path?: WritableLogStream | FileDescriptor | string, logLevel?: LogLevels): Logger { + return new Logger(log_file_path, logLevel); };