morgan


1. Simple Request and Response Logging

const morgan = require('morgan');

const app = express();
app.use(morgan('tiny'));

2. Custom Logging Format

const morgan = require('morgan');

const app = express();
app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms'));

3. Ignore Certain Routes

const morgan = require('morgan');
const ignore = ['/favicon.ico'];

const app = express();
app.use(morgan('tiny', { skip: (req, res) => ignore.includes(req.url) }));

4. Log to a File

const morgan = require('morgan');
const fs = require('fs');
const path = require('path');

const app = express();
const logfile = fs.createWriteStream(path.join(__dirname, 'access.log'));
app.use(morgan('combined', { stream: logfile }));

5. Log to Multiple Destinations

6. Exclude Sensitive Information

7. Format Log Message

8. Skip Logging for Static Files

9. Log HTTP Errors

10. Log Request and Response Body

11. Include Header Information

12. Log Request Duration

13. Customize Token Format

14. Log API Response Time

15. Ignore Logging for Health Checks

16. Tokenize Custom Middleware

17. Use Morgan with Passport

18. Log Exception Details

19. Log Query Parameters

20. Log Request Body

21. Log Response Headers

22. Log Database Queries

23. Log Redis Commands

24. Log MongoDB Queries

25. Log Kafka Messages