Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelooblan2016 committed Oct 26, 2022
0 parents commit 707d916
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
35 changes: 35 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const fs = require('fs-extra');
const { type } = require('jquery');
const moment = require('moment');

class Plogger {
constructor (options) {
this.logFileName = options[`logFileName`];
this.dateFormat = options[`dateFormat`] ?? `YYYY-MM-DD hh:mm:ssA`;
}
/**
* Write message to the log file with details
* Sample: [2022-10-05 11:03:44] local.INFO: YV4A22RL4M1768603
* @param {*} message
* @param {*} prefix
* @return boolean
*/
log (message, prefix = 'INFO') {
try {
let content = [
`[${moment().format(this.dateFormat)}]`,
`${prefix}:`,
typeof message == 'object' ? JSON.stringify(message) : message,
].join(" ");

fs.appendFileSync(this.logFileName, `${content}\n`);
return true;
} catch (error) { return false; }
}

info (message, prefix = 'INFO') { return this.log(message, prefix); }
error (message, prefix = 'ERROR') { return this.log(message, prefix); }
warning (message, prefix = 'WARNING') { return this.log(message, prefix); }
}

module.exports = Plogger;
112 changes: 112 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "mrx-plogger",
"version": "1.0.0",
"description": "Nodejs storage log",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "marx",
"license": "ISC",
"dependencies": {
"fs-extra": "^9.1.0",
"moment": "^2.29.4"
}
}

0 comments on commit 707d916

Please sign in to comment.