diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d92c3376a6bc96d96902ead4889b04f647a23859 --- /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 0000000000000000000000000000000000000000..6cc5dfecb0baf18daffe674e811ea39de97a8f01 --- /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 0000000000000000000000000000000000000000..46bcba50d4c30d0fd29407bca501b35da75ed4df --- /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 0000000000000000000000000000000000000000..590e0365ba62cafd9d92a89238c82b211a8b8abd --- /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 0000000000000000000000000000000000000000..4737b0991c28ccd7e1b4dd21b1bda877b0f62d38 --- /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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391