{"action":"created","issue":{"url":"https://api.github.com/repos/expressjs/morgan/issues/65","labels_url":"https://api.github.com/repos/expressjs/morgan/issues/65/labels{/name}","comments_url":"https://api.github.com/repos/expressjs/morgan/issues/65/comments","events_url":"https://api.github.com/repos/expressjs/morgan/issues/65/events","html_url":"https://github.com/expressjs/morgan/issues/65","id":82984334,"number":65,"title":"JSON log format","user":{"login":"nodesocket","id":523312,"avatar_url":"https://avatars.githubusercontent.com/u/523312?v=3","gravatar_id":"","url":"https://api.github.com/users/nodesocket","html_url":"https://github.com/nodesocket","followers_url":"https://api.github.com/users/nodesocket/followers","following_url":"https://api.github.com/users/nodesocket/following{/other_user}","gists_url":"https://api.github.com/users/nodesocket/gists{/gist_id}","starred_url":"https://api.github.com/users/nodesocket/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nodesocket/subscriptions","organizations_url":"https://api.github.com/users/nodesocket/orgs","repos_url":"https://api.github.com/users/nodesocket/repos","events_url":"https://api.github.com/users/nodesocket/events{/privacy}","received_events_url":"https://api.github.com/users/nodesocket/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/expressjs/morgan/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":{"login":"dougwilson","id":67512,"avatar_url":"https://avatars.githubusercontent.com/u/67512?v=3","gravatar_id":"","url":"https://api.github.com/users/dougwilson","html_url":"https://github.com/dougwilson","followers_url":"https://api.github.com/users/dougwilson/followers","following_url":"https://api.github.com/users/dougwilson/following{/other_user}","gists_url":"https://api.github.com/users/dougwilson/gists{/gist_id}","starred_url":"https://api.github.com/users/dougwilson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dougwilson/subscriptions","organizations_url":"https://api.github.com/users/dougwilson/orgs","repos_url":"https://api.github.com/users/dougwilson/repos","events_url":"https://api.github.com/users/dougwilson/events{/privacy}","received_events_url":"https://api.github.com/users/dougwilson/received_events","type":"User","site_admin":false},"milestone":null,"comments":2,"created_at":"2015-05-31T03:31:00Z","updated_at":"2015-05-31T07:08:24Z","closed_at":null,"body":"What is the best way to write out JSON to `stdout`? For example:\r\n\r\n````\r\n{\r\n    \"date\": \"2015-05-30T13:55:36.000Z\",\r\n    \"http_version\": \"1.1\",\r\n    \"method\": \"GET\",\r\n    \"url\": \"/\",\r\n    \"status\": 200,\r\n    \"response_time\": 75.564,\r\n    \"remote_user\": null,\r\n    \"remote_addr\": \"43.34.54.121\",\r\n    \"user_agent\": \"curl/7.37.1\",\r\n    \"referrer\": null\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/expressjs/morgan/issues/comments/107135214","html_url":"https://github.com/expressjs/morgan/issues/65#issuecomment-107135214","issue_url":"https://api.github.com/repos/expressjs/morgan/issues/65","id":107135214,"user":{"login":"nodesocket","id":523312,"avatar_url":"https://avatars.githubusercontent.com/u/523312?v=3","gravatar_id":"","url":"https://api.github.com/users/nodesocket","html_url":"https://github.com/nodesocket","followers_url":"https://api.github.com/users/nodesocket/followers","following_url":"https://api.github.com/users/nodesocket/following{/other_user}","gists_url":"https://api.github.com/users/nodesocket/gists{/gist_id}","starred_url":"https://api.github.com/users/nodesocket/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nodesocket/subscriptions","organizations_url":"https://api.github.com/users/nodesocket/orgs","repos_url":"https://api.github.com/users/nodesocket/repos","events_url":"https://api.github.com/users/nodesocket/events{/privacy}","received_events_url":"https://api.github.com/users/nodesocket/received_events","type":"User","site_admin":false},"created_at":"2015-05-31T07:08:24Z","updated_at":"2015-05-31T07:08:24Z","body":"Trying:\r\n\r\n````\r\napp.use(morgan(format({\r\n        id: ':id',\r\n        date: ':date[iso]',\r\n        http_version: ':http-version',\r\n        method: ':method',\r\n        url: ':url',\r\n        status: ':status',\r\n        response_time: ':response-time',\r\n        remote_user: ':remote-user',\r\n        remote_addr: ':remote-addr',\r\n        user_agent: ':user-agent',\r\n        referrer: ':referrer'\r\n    })));\r\n\r\n    function format(obj) {\r\n        var keys = Object.keys(obj);\r\n        var token = /^:([-\\w]{2,})(?:\\[([^\\]]+)\\])?$/;\r\n\r\n        return function (tokens, req, res) {\r\n            var data = {};\r\n            for (var i = 0; i < keys.length; i++) {\r\n                var val = token.exec(obj[key]);\r\n                data[key] = val !== null ? tokens[val[1]](req, res, val[2]) : obj[key];\r\n            }\r\n\r\n            r.table('requests').insert(data, { returnChanges: true }).run(rethinkdbConnection, function(error, result) {\r\n                if(error) {\r\n                    handleError(res, error);\r\n                }\r\n                else if (result.inserted !== 1) {\r\n                    handleError(res, new Error(\"Failed to insert request log document into RethinkDB.\"));\r\n                } else {\r\n                    next();\r\n                }\r\n            });\r\n        };\r\n    }\r\n```\r\n\r\nGetting:\r\n\r\n````\r\n                var val = token.exec(obj[key]);\r\n                                         ^\r\nReferenceError: key is not defined\r\n````"}}