logmyerr
v1.1.2
Published
LogMyErr plugin will handle the logging of errors, warnings and info
Downloads
21
Maintainers
Readme
LogMyErr
LogMyErr JS Module enables write logs in log file or text file in server side.
Features:
- Uses the custom log message to output information and write log to the text file of log file/ in custom extention.
- Exception Handler allows us to handle exception from the route file in MVC.
Please report bugs there on Github.
Implementing Logger and Custom Logger
const {logger} = require('logmyerr');
logger.write("Hello, World");
logger will creates a
log
directory in your local folder and create it automatically create a folder log withlogmyerr-dd-mm-yy.log
format in base project directorylogger.write
this methods takes different parameters.message
- required parameter, This takes your message or a information. type :String
, default value :No
type
- optional parameter, it can accept only theInfo
/error
/warn
/debug
. type :String
, default value :info
- Example
logger.LOGTYPE.info
logger.LOGTYPE.error
logger.LOGTYPE.warn
logger.LOGTYPE.debug
- Example
loggers.write(err.stack,loggers.LOGTYPE.info);
priority
- optional parameter, it can accept only thecritical
/informative
/major
. type :String
, default value :informative
- Example
this.PRIORITY.critical
this.PRIORITY.informative
this.PRIORITY.major
- Example
loggers.write(err.stack,loggers.LOGTYPE.info, loggers.PRIORITY.informative);
OR
const {customLogger} = require('logmyerr');
const logger = customLogger.getInstance();
logger.write("Hello, World");
- In this
customLogger
it help us to change the default configuration.
Example
const {customLogger} = require('logmyerr');
let defaultConfig = {
logFileName: "logmyerr" ,
logFileExtension: ".log",
logFilePath: "<Your System local path or a Custom Path>",
logFolderName: "logs",
}
const logger = customLogger.getInstance(defaultConfig);
logger.write("Hello, World");
logger will creates a
logs
directory in custom local folder and create it automatically create a folder log withlogmyerr-dd-mm-yy.log
format in base project directorylogger.write
this methods takes different parameters.message
- required parameter, This takes your message or a information. type :String
, default value :No
type
- optional parameter, it can accept only theInfo
/error
/warn
/debug
. type :String
, default value :info
- Example
logger.LOGTYPE.info
logger.LOGTYPE.error
logger.LOGTYPE.warn
logger.LOGTYPE.debug
- Example
loggers.write(err.stack,loggers.LOGTYPE.info);
priority
- optional parameter, it can accept only thecritical
/informative
/major
. type :String
, default value :informative
- Example 1)this.PRIORITY.critical
2)this.PRIORITY.informative
3)this.PRIORITY.major
loggers.write(err.stack,loggers.LOGTYPE.info, loggers.PRIORITY.informative);
Exception Handler Router based
If you are following MVC patter in you Application you can use this exception handler to Handle all your exception from your router. Basically It will handles the all the exception from your controller.
let express = require('express');
let router = express.Router();
let {exceptionHandler} = require('logmyerr');
const LoginController = require('../controllers/login.controllers');
router.post('/', exceptionHandler(LoginController.login));
and also it must to be implement the global exception handler in main.js
file
Example
📁 app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
// importing DotEnv
require('dotenv').config();
let {logger} = require('logmyerr');
let loginRouter = require('./routes/login.routes');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/login', loginRouter);
app.use(function(req, res, next) {
if(process.env.NODE_ENV === 'development'){
next(createError(404));
}else if(process.env.NODE_ENV === 'Perf'){
next(createError(404));
}else{
const err = new Error('Not Found');
err.status = 404;
next(err);
}
});
// error handler
app.use(function(err, req, res, next) {
if(process.env.NODE_ENV === 'development'){
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
loggers.write(err.stack,loggers.LOGTYPE.error, loggers.PRIORITY.critical);
// render the error page
res.status(err.status || 500);
res.json({
message: err.message,
});
}
else{
res.status (err.status || 500);
loggers.write(err.stack,loggers.LOGTYPE.error, loggers.PRIORITY.critical);
res.json({
error: {
message: "Internal Server Error"
}
});
}
});
module.exports = app;
📶 router.js
let express = require('express');
let router = express.Router();
let {exceptionHandler} = require('logmyerr');
const LoginController = require('../controllers/login.controllers');
router.post('/', exceptionHandler(LoginController.login));
🎮 Controller.js
const LoginController = {}
LoginController.login = (req, res) => {
throw new Error("Dummy Exception");
}