Skip to content

Instantly share code, notes, and snippets.

View ManotLuijiu's full-sized avatar
🏠
Working from home

Manot Luijiu ManotLuijiu

🏠
Working from home
View GitHub Profile
@ManotLuijiu
ManotLuijiu / settings.json
Created May 31, 2021 01:42
.vscode configuration
{
"editor.fontSize": 16,
"terminal.integrated.fontSize": 16,
"window.zoomLevel": 0
}
@ManotLuijiu
ManotLuijiu / logger.js
Created June 5, 2021 10:23 — forked from ludwig/logger.js
winston logger with filename:linenumber
// NOTE: this adds a filename and line number to winston's output
// Example output: 'info (routes/index.js:34) GET 200 /index'
var winston = require('winston')
var path = require('path')
var PROJECT_ROOT = path.join(__dirname, '..')
var logger = new winston.logger({ ... })
// this allows winston to handle output from express' morgan middleware
@ManotLuijiu
ManotLuijiu / colorful_winston_logger.js
Created June 5, 2021 10:30 — forked from alfasin/colorful_winston_logger.js
A Nodejs implementation of a console transport logger based on Winston 3.x which also prints the filename and line-number
const winston = require('winston');
const { format } = winston;
const { combine, colorize, timestamp, printf } = format;
/**
* /**
* Use CallSite to extract filename and number, for more info read: https://v8.dev/docs/stack-trace-api#customizing-stack-traces
* @param numberOfLinesToFetch - optional, when we want more than one line back from the stacktrace
* @returns {string|null} filename and line number separated by a colon, if numberOfLinesToFetch > 1 we'll return a string
@ManotLuijiu
ManotLuijiu / loggerWithLine.js
Created June 6, 2021 22:07
Log node.js project with winston and morgan
const winston = require('winston');
require('winston-daily-rotate-file');
const path = require('path');
const PROJECT_ROOT = path.join(__dirname, '..');
const highlight = require('cli-highlight').highlight;
// const options = {
// file: {
// level: 'info',
@ManotLuijiu
ManotLuijiu / app.js
Last active June 12, 2021 15:06
Log node.js project with winston and morgan
const express = require('express');
const bodyParser = require('body-parser');
const favicon = require('serve-favicon');
const path = require('path');
const rfs = require('rotating-file-stream');
require('dotenv').config(); // Put before logger.js
// const logger = require('./util/loggerEasy');
const logger = require('./util/logger');
const { stream } = logger;
@ManotLuijiu
ManotLuijiu / app.js
Last active June 7, 2021 08:34
Log node.js project with winston and morgan
const express = require('express');
const bodyParser = require('body-parser');
const favicon = require('serve-favicon');
const path = require('path');
const rfs = require('rotating-file-stream');
// const logger = require('./util/loggerEasy');
const logger = require('./util/loggerWithLineNew');
const { stream } = logger;
const morgan = require('morgan');
@ManotLuijiu
ManotLuijiu / logger.js
Last active May 3, 2025 12:30
Print filename and line-number to node's log with winston and morgan
const winston = require('winston');
require('winston-daily-rotate-file');
require('winston-mongodb');
const path = require('path');
const PROJECT_ROOT = path.join(__dirname, '..');
const highlight = require('cli-highlight').highlight;
const arrow = '\u276F\u276F\u25B6';
const logConfig = {
@ManotLuijiu
ManotLuijiu / .env.example
Created June 7, 2021 17:58
How to put MongoDB URI in .env file
MONGO_URI='mongodb+srv://<user>:<password>@<cluster_name>.xxxxx.mongodb.net/<database_name>?retryWrites=true&w=majority'
@ManotLuijiu
ManotLuijiu / errorResponse.js
Created June 16, 2021 16:09
ES6 Custom Error Class
class ErrorResponse extends Error {
constructor(message, statusCode, reason) {
super(message);
this.statusCode = statusCode;
this.reason = reason;
this.name = 'ErrorResponse';
}
}
module.exports = ErrorResponse;
@ManotLuijiu
ManotLuijiu / error.js
Created June 17, 2021 06:28
Error handler for NodeJS Project
const ErrorResponse = require('./errorResponse');
const logger = require('./logger');
const colors = require('colors');
colors.enable();
const errorHandler = (err, req, res, next) => {
let error = { ...err };
error.message = err.message;
// logger.error(err.stack.red);
console.log(err.stack.red);