From cb39eaf131c0d8877198b27cfd7d803de21f6d3d Mon Sep 17 00:00:00 2001 From: <sigmundd@DESKTOP-0HO7Q14.localdomain> Date: Wed, 26 Sep 2018 15:31:45 +0200 Subject: [PATCH] Initial --- .gitignore | 2 ++ README.md | 23 ++++++++++++++++ index.js | 70 +++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 18 ++++++++++++ package.json | 15 ++++++++++ tests/test.js | 0 6 files changed, 128 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 index.js create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 tests/test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d92c337 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +samples/file.log \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6cc5dfe --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# log.js + +A real simple logger application + +## Usage + +`const log = require('log')(options)` + +Note: The options Part may be omitted, as all parts are optional, but using the name is recommended, as without it, the folder-name will be used + +Options: + +{ + name: 'Name of App', + hostname: 'Server Hostname', + file: 'File to Append Log to', + graylog: [ + { + server: 'graylog-server', + port: 'graylog-port' + } + ] +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..46bcba5 --- /dev/null +++ b/index.js @@ -0,0 +1,70 @@ +module.exports = function (options) { + this.moment = require('moment') + this.path = require('path') + this.fs = require('fs') + this.os = require('os') + this.options = options || {} + this.name = (this.options.name) ? this.options.name : this.path.basename(this.path.dirname(require.main.filename)) + this.hostname = (this.options.hostname) ? this.options.hostname : this.os.hostname() + if (this.options.graylog) { + this.graylog2 = require('graylog2') + this.graylogger = new this.graylog2.graylog({ // eslint-disable-line new-cap + servers: this.options.graylog, + hostname: this.hostname, + facility: this.name + }) + } + this.info = function (message) { + this.log('INFO', message) + } + this.warn = function (message) { + this.log('WARN', message) + } + this.error = function (message) { + this.log('ERROR', message) + } + this.debug = function (message) { + this.log('DEBUG', message) + } + this.log = function (tag, message) { + let msg = this.moment().format('YYYY-MM-DDTHH:mm:ss') + '\t' + this.hostname + '\t' + this.name + '\t' + tag + '\t' + message + switch (tag) { + case 'INFO': + console.log(msg) + break + case 'WARN': + console.warn(msg) + break + case 'ERROR': + console.error(msg) + break + case 'DEBUG': + console.log(msg) + break + default: + console.log(msg) + } + if (this.options.file) { + this.fs.appendFileSync(this.options.file, msg + '\n') + } + if (this.options.graylog) { + switch (tag) { + case 'INFO': + this.graylogger.info(msg) + break + case 'WARN': + this.graylogger.warning(msg) + break + case 'ERROR': + this.graylogger.error(msg) + break + case 'DEBUG': + this.graylogger.debug(msg) + break + default: + this.graylogger.notice(msg) + } + } + } + return this +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..590e036 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,18 @@ +{ + "name": "log", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "graylog2": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/graylog2/-/graylog2-0.2.1.tgz", + "integrity": "sha512-vjysakwOhrAqMeIvSK0WZcmzKvkpxY6pCfT9QqtdSVAidPFIynuin7adqbdFp9MCCTbTE402WIxvg8cph5OWTA==" + }, + "moment": { + "version": "2.22.2", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.22.2.tgz", + "integrity": "sha1-PCV/mDn8DpP/UxSWMiOeuQeD/2Y=" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..4737b09 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "log", + "version": "1.0.0", + "description": "A simple Logger with Options!", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Dominik Sigmund", + "license": "ISC", + "dependencies": { + "graylog2": "^0.2.1", + "moment": "^2.22.2" + } +} diff --git a/tests/test.js b/tests/test.js new file mode 100644 index 0000000..e69de29 -- GitLab