Skip to content

Instantly share code, notes, and snippets.

@shinaisan
Last active August 25, 2018 07:17
Show Gist options
  • Save shinaisan/4b053ae85a55b3db9d9193ea0e0a95b8 to your computer and use it in GitHub Desktop.
Save shinaisan/4b053ae85a55b3db9d9193ea0e0a95b8 to your computer and use it in GitHub Desktop.
Example of morgan tiny, short, common and other formats
# See https://help.github.com/ignore-files/ for more about ignoring files.
# dependencies
/node_modules
# testing
/coverage
# production
/dist
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
.*.swp
npm-debug.log*
yarn-debug.log*
yarn-error.log*
tmp
var
public
access.log

Example of morgan tiny, short, common and other formats

Examples of morgan output.

tiny

Format:

:method :url :status :res[content-length] - :response-time ms

Example:

GET /get/cat 200 6 - 3.491 ms
POST /post/cat/animal 200 2 - 0.553 ms
GET /get/cat 200 6 - 0.273 ms
GET /nowhere 404 146 - 5.503 ms
GET /get/nothing 404 18 - 2.206 ms

short

Format:

:remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms

Example:

::ffff:127.0.0.1 - GET /get/cat HTTP/1.1 200 6 - 3.497 ms
::ffff:127.0.0.1 - POST /post/cat/animal HTTP/1.1 200 2 - 0.548 ms
::ffff:127.0.0.1 - GET /get/cat HTTP/1.1 200 6 - 0.273 ms
::ffff:127.0.0.1 - GET /nowhere HTTP/1.1 404 146 - 3.436 ms
::ffff:127.0.0.1 - GET /get/nothing HTTP/1.1 404 18 - 0.245 ms

common

Format:

:remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]

Example:

::ffff:127.0.0.1 - - [25/Aug/2018:02:00:53 +0000] "GET /get/cat HTTP/1.1" 200 6
::ffff:127.0.0.1 - - [25/Aug/2018:02:00:56 +0000] "POST /post/cat/animal HTTP/1.1" 200 2
::ffff:127.0.0.1 - - [25/Aug/2018:02:01:00 +0000] "GET /get/cat HTTP/1.1" 200 6
::ffff:127.0.0.1 - - [25/Aug/2018:02:01:03 +0000] "GET /nowhere HTTP/1.1" 404 146
::ffff:127.0.0.1 - - [25/Aug/2018:02:01:05 +0000] "GET /get/nothing HTTP/1.1" 404 18

dev

Format:

:method :url :status :response-time ms - :res[content-length]

Example:

GET /get/cat 200 3.632 ms - 6
POST /post/cat/animal 200 0.580 ms - 2
GET /get/cat 200 0.273 ms - 6
GET /nowhere 404 3.434 ms - 146
GET /get/nothing 404 0.244 ms - 18

combined

Format:

:remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"

Example:

::ffff:127.0.0.1 - - [25/Aug/2018:02:03:37 +0000] "GET /get/cat HTTP/1.1" 200 6 "-" "curl/7.35.0"
::ffff:127.0.0.1 - - [25/Aug/2018:02:03:40 +0000] "POST /post/cat/animal HTTP/1.1" 200 2 "-" "curl/7.35.0"
::ffff:127.0.0.1 - - [25/Aug/2018:02:03:47 +0000] "GET /get/cat HTTP/1.1" 200 6 "-" "curl/7.35.0"
::ffff:127.0.0.1 - - [25/Aug/2018:02:03:52 +0000] "GET /nowhere HTTP/1.1" 404 146 "-" "curl/7.35.0"
::ffff:127.0.0.1 - - [25/Aug/2018:02:03:54 +0000] "GET /get/nothing HTTP/1.1" 404 18 "-" "curl/7.35.0"

References

const express = require('express');
const fs = require('fs');
const path = require('path');
const morgan = require('morgan');
const loggerType = 'combined';
const accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), {flags: 'a'});
const app = express();
app.use(morgan(loggerType, { stream: accessLogStream }));
let dict = {
hello: 'greeting',
greeting: 'acknowledgement',
cat: 'feline',
feline: 'carnivore',
placental: 'mammal'
};
app.get('/', (req, res) => res.send('Hello World!'));
app.get('/get/:key', (req, res) => {
const key = req.params.key;
const value = dict[key];
if (!value) {
res.status(404).send(key + ' not found.');
return;
}
res.send(value);
});
app.post('/post/:key/:value', (req, res) => {
const key = req.params.key;
const value = req.params.value;
if ((!key) || (!value)) {
res.status(500).send('Bad request.');
return;
}
dict[key] = value;
res.status(200).send('OK');
});
app.use(express.static('public'));
app.listen(3000, () => console.log('Example app listening on port 3000!'));
{
"name": "morgan-tiny-short-common-formats",
"version": "1.0.0",
"description": "Example of morgan tiny, short, common and other formats",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"JavaScript",
"npm",
"logger",
"morgan",
"Express"
],
"author": "[email protected]",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"morgan": "^1.9.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment