Skip to content
Snippets Groups Projects
Commit cb39eaf1 authored by 's avatar
Browse files

Initial

parent fa899822
Branches
Tags
1 merge request!1Update index.test.js, examples/all-options.js, examples/log-to-file.js,...
node_modules
samples/file.log
\ No newline at end of file
# 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
index.js 0 → 100644
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
}
{
"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="
}
}
}
{
"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"
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment