{"id":17983492,"url":"https://github.com/techjacker/log-errors","last_synced_at":"2025-04-04T02:13:02.159Z","repository":{"id":7976706,"uuid":"9378969","full_name":"techjacker/log-errors","owner":"techjacker","description":"Node.js error logger, can be used as standalone or with express.js","archived":false,"fork":false,"pushed_at":"2013-10-07T19:35:09.000Z","size":260,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-20T04:08:38.350Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/techjacker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-04-11T19:52:25.000Z","updated_at":"2016-09-28T22:24:50.000Z","dependencies_parsed_at":"2022-09-04T21:01:55.756Z","dependency_job_id":null,"html_url":"https://github.com/techjacker/log-errors","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/techjacker%2Flog-errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/techjacker%2Flog-errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/techjacker%2Flog-errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/techjacker%2Flog-errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/techjacker","download_url":"https://codeload.github.com/techjacker/log-errors/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247107828,"owners_count":20884797,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-29T18:17:24.136Z","updated_at":"2025-04-04T02:13:02.143Z","avatar_url":"https://github.com/techjacker.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Log Errors\n\n[![Build Status](https://secure.travis-ci.org/techjacker/log-errors.png)](http://travis-ci.org/techjacker/log-errors)\n\n- logs errors based on environment\n    - development gets full color-coded errors\n    - production error logs use syslog error headings so they will be picked up by logwatch et al\n    - production only logs serious errors (level 3 and below) to reduce noise\n- request attrs logged if passed request object\n- response returned if passed response object\n\n## Quickstart\n\n```JavaScript\nvar\tlogErrors \t= require('log-errors'),\n\tlogProd \t= logErrors.production,\n\tlogDev \t\t= logErrors.development;\n\ntry {\n\tthrow new error(\"funky\");\n} catch (e) {\n\tlogDev(e);\n}\n```\n\nThis will output in colored text:\n\n```Shell\nError name: Error\n\nError object:\n\n{  \tError: \t\tfunky,\n   \tmessage: \t'funky',\n   \ttype: \t\tundefined,\n   \tstack:  \tGetter/Setter,\n   \targuments: \tundefined }\n\nStack trace:\n\n'Error: funky\n    at Object.\u003canonymous\u003e (/home/andy/lib/modules/npm/log-errors/lib/development.js:16:12)\n    at Module._compile (module.js:449:26)\n    at Object.Module._extensions..js (module.js:467:10)\n    at Module.load (module.js:356:32)\n    at Function.Module._load (module.js:312:12)\n    at Module.runMain (module.js:492:10)\n    at process.startup.processNextTick.process._tickCallback (node.js:244:9)'\n```\n\n### Using in Express.js\n\nIt will also log request url + query info.\n\n```JavaScript\nvar\tlogErrors \t= require('log-errors'),\n\tlogProd \t= logErrors.production,\n\tlogDev \t\t= logErrors.development;\n\n\n// add this at very bottom (below all route handlers)\n// it is designed to catch the errors passed by next(err) calls\n\napp.configure('production', function() {\n\tapp.use(logProd);\n});\n\napp.configure('development', function() {\n\tapp.use(logDev);\n\t// equates to:\n\t// app.use(function(err, req, res, next) {\n\t\t// logDev(err, req, res, next);\n\t// });\n});\n```\n\nExample Output:\n\n```Shell\nError name: Error\n\nRequest:\n\nurl: someurl\nquery: ?some=random\u0026query=params\n\nError object:\n\n{  \tError: \t\tfunky,\n   \tmessage: \t'funky',\n   \ttype: \t\tundefined,\n   \tstack:  \tGetter/Setter,\n   \targuments: \tundefined }\n\nStack trace:\n\n'Error: funky\n    at Object.\u003canonymous\u003e (/home/andy/lib/modules/npm/log-errors/lib/development.js:16:12)\n    at Module._compile (module.js:449:26)\n    at Object.Module._extensions..js (module.js:467:10)\n    at Module.load (module.js:356:32)\n    at Function.Module._load (module.js:312:12)\n    at Module.runMain (module.js:492:10)\n    at process.startup.processNextTick.process._tickCallback (node.js:244:9)'\n```\n\n## Docs\n\n#### Info printed to STDOUT\n1. error name: (err.name)\n2. error message (err.message)\n3. error logLevel (err.logLevel)\n4. error responseCode (err.resCode)\n5. request url (req.url)\n6. request query (req.query)\n\n\n#### HTTP Response\n\nIf passed a response object then the logger will return this with the response code specified in the error (err.resCode). It defaults to sending a 500 server error with the body {error: 'Error'}.\n\n```JavaScript\nvar logErrors   = require('log-errors'),\n    logDev      = logErrors.development;\n\n  // will call res.send(403, {\"error\":\"random\"})\n  logDev({resCode:403, name:\"random\"}, req, res);\n```\n\n\n#### Log Levels\n- Must be one of the 8 unix log levels used in the [Visionmedia Logging Module](https://github.com/visionmedia/log.js).\n- Defaults to 'info' level if not passed one of the 8 listed.\n\n\n### Base Log Class\n- Production and development loggers inherit from this.\n- If app is an eventEmitter then the 'seriousError' evt will be prodcast when using the productionLogger.\n\n```JavaScript\nvar LogClass = function (env, app) {\n    this.env = env;\n    this.app = app;\n};\n```\n\n\n### Development Logger\n- Always prints full error in colored text.\n\n```JavaScript\nvar logDev \t= require('log-errors').development;\nlogDev(new Error('development error msg'));\n```\n\n### Production Logger\n- Will only print error info if error.LogLevel is 3 or below, ie ['error', 'critical', 'alert', 'emergency']\n\n```JavaScript\nvar logProd = require('log-errors').production,\n    err     = newError('some message about the error');\n\nerr.logLevel = 'critical'\nlogProd(err);\n```\n\n\n#### 'seriousError' Evt\n- If error.LogLevel is 3 or below and this.app is an eventEmitter then the 'seriousError' evt will be prodcast when using the productionLogger.\n- It will NOT be broadcast if the error.doNotKill is truthy\n\n```JavaScript\n    if (!err.doNotKill) {\n      appErrorHandler \u0026\u0026 this.app.emit('seriousError');\n    }\n```\n\n\n## Using in Conjunction with [Custom Errors npm Module](https://github.com/techjacker/custom-errors)\n\n- If your errors inherit from the custom [errors class](https://github.com/techjacker/custom-errors) then the extra error attrs (logLevel, name, message etc) are already added.\n- However, the logger should work fine with the built in base error class too.\n\n```JavaScript\nvar valErr \t\t= require('custom-errors').general.ValidationError,\n\tlogErrors \t= require('log-errors'),\n\tlogProd \t= logErrors.production,\n\tlogDev \t\t= logErrors.development;\n\ntry {\n\tthrow new valErr(\"funky\");\n} catch (e) {\n\tlogDev(e);\n}\n```\n\nOutputs:\n\n```Shell\nError name: Validation\n\nError object:\n\n{ logLevel: 'warn',\n  message: 'funky',\n  name: 'Validation',\n  resCode: 400,\n  [stack]: [Getter/Setter] }\n\nStack trace:\n\n'Validation: funky\n    at Object.\u003canonymous\u003e (/home/andy/lib/modules/npm/log-errors/lib/development.js:18:12)\n    at Module._compile (module.js:449:26)\n    at Object.Module._extensions..js (module.js:467:10)\n    at Module.load (module.js:356:32)\n    at Function.Module._load (module.js:312:12)\n    at Module.runMain (module.js:492:10)\n    at process.startup.processNextTick.process._tickCallback (node.js:244:9)'\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftechjacker%2Flog-errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftechjacker%2Flog-errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftechjacker%2Flog-errors/lists"}