{"id":25084138,"url":"https://github.com/ncounter/loggerhead","last_synced_at":"2025-10-11T22:01:59.336Z","repository":{"id":57156712,"uuid":"144066768","full_name":"ncounter/loggerhead","owner":"ncounter","description":"A simple plain Javascript plugin to log frontend messages to a server.","archived":false,"fork":false,"pushed_at":"2019-01-28T20:16:36.000Z","size":81,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-05T03:33:43.381Z","etag":null,"topics":["event-listener","event-log","javascript","javascript-logger","javascript-plugin","js","loggerhead"],"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/ncounter.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":"2018-08-08T20:56:45.000Z","updated_at":"2025-05-21T20:33:53.000Z","dependencies_parsed_at":"2022-08-30T05:31:53.549Z","dependency_job_id":null,"html_url":"https://github.com/ncounter/loggerhead","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ncounter/loggerhead","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ncounter%2Floggerhead","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ncounter%2Floggerhead/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ncounter%2Floggerhead/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ncounter%2Floggerhead/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ncounter","download_url":"https://codeload.github.com/ncounter/loggerhead/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ncounter%2Floggerhead/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268021662,"owners_count":24182706,"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","status":"online","status_checked_at":"2025-07-31T02:00:08.723Z","response_time":66,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["event-listener","event-log","javascript","javascript-logger","javascript-plugin","js","loggerhead"],"created_at":"2025-02-07T06:46:23.428Z","updated_at":"2025-10-11T22:01:59.253Z","avatar_url":"https://github.com/ncounter.png","language":"JavaScript","readme":"# Loggerhead\nA simple plain Javascript plugin to send frontend log messages to a server.\n\nNote:\n\n`Loggerhead`, a part from the [current main version](https://github.com/ncounter/loggerhead) is also available in different versions:\n - **npm module** at the [npm repository](https://www.npmjs.com/package/loggerhead-module)\n - **npm module** at the [git repository](https://github.com/ncounter/loggerhead/tree/master/loggerhead-module) - *which is just an internal subfolder of the current `Loggerhead.js` git repo project*\n - **old-browser-version** with no dependencies on `fetch`, `Promise` and other fresh supported features, at the [git repository](https://github.com/ncounter/loggerhead/tree/master/for-old-browser) - *which is just an internal subfolder of the current `Loggerhead.js` git repo project*\n\n## How it works\n`Loggerhead.js` sends a log message to a configurable endpoint URL by a POST request each time one of the `log level` function is called.\n\n### Log levels\nLog levels are\n* `info`\n* `debug`\n* `warning`\n* `error`\n\nBy default all levels are enabled. Whenever a log level function is called, it receives a string message, it prepares a `POST` request and it forwards the message to the set `URL`, injecting in the `POST` data the `log level` information. At the same time, all the same correspondant levels are available and enabled for the `console` to be shown. The enable/disable can be toggled by config parameters.\n\n**Note**: each log level function returns the `Promise` that sends the `POST` request, this way it is possible to add a `.then()` slice in order to apply some other action on the `response` (if any) from the server after storing the log message. See an example below:\n```javascript\nLoggerhead.info('Send this info log message to the server')\n    .then(serverResponse =\u003e alert(serverResponse.message))\n    .catch(error =\u003e alert(serverResponse.error));\n```\n\n### Headers\n`Loggerhead.js` provides also a way to customize `headers` values of the `POST` request: this can be used to add some **authentication** parameters, for instance. The default method behaves like a `proxy` receiving and returning the default `headers` map (*). In order to add more `headers` parameters this method can be overridden by a function that receives and returns the `headers` map as well, but it does something in the middle. See below an example:\n\n```javascript\n// this is the default method\nLoggerhead.setHeaders = function(headers) {\n  return headers;\n}\n\n// this overrides the default method adding the `X-CSRF-Token` parameters in the `headers` map\nLoggerhead.setHeaders = function(headers) {\n  headers.set('X-CSRF-Token', '\u003cmy-token-value\u003e');\n  return headers;\n}\n```\n\n(*) Note: by default `headers` contains only `{'Content-Type': 'application/json; charset=utf-8')}`\n\n## How to use\n```html\n\u003cscript type=\"text/javascript\" src=\"loggerhead.js\"\u003e\u003c/script\u003e\n\n\u003cscript type=\"text/javascript\"\u003e\n  /* Minimal code to get Loggerhead working properly */\n  Loggerhead.set({ url: 'https://httpbin.org/post' });\n\n  /* Let's use Loggerhead functions to send some log messages */\n  Loggerhead.info('This is an info log message');\n  Loggerhead.warning('This is an warning log message')\n      .then(confirm =\u003e alert(confirm))\n      .catch(error =\u003e alert(error));\n\n  /* Let's disable debug log level */\n  Loggerhead.set({ levels: { debug : false }});\n\n  /* Let's disable info and debug levels for the console */\n  Loggerhead.set({ console: { info : false, debug: false }});\n\u003c/script\u003e\n```\n\n## Use case\nThis `plugin` can be used in a browser scenario, adding `listeners` for specific `events` and `callback` log functions in order to send and store log messages about the `event` that just triggered the `Loggerhead`.\n\n```javascript\n  // store a log message about the page has been loaded\n  window.addEventListener('load', function(event) {\n    Loggerhead.info('[' + new Date().toUTCString() + '] - Loading `' + window.location + '`');\n  });\n  // store a log message about a page has been left\n  window.addEventListener('unload', function(event) {\n    Loggerhead.info('[' + new Date().toUTCString() + '] - Leaving `' + window.location + '`');\n  });\n  // store a log message about the error that just happened\n  window.addEventListener('error', function(event) {\n    // Note that col \u0026 error are new to the HTML 5 and may not be supported in every browser.\n    var extra = !event.colno ? '' : '\\ncolumn: ' + event.colno;\n    extra += !event.error ? '' : '\\nerror: ' + event.error;\n    const errorMessage = event.message + '\\nurl: ' + event.filename + '\\nline: ' + event.lineno + extra;\n    Loggerhead.error(errorMessage);\n  });\n```\n\n## Config parameters\n```javascript\n// the server endpoint where to send logs\nurl: String,\n\n// log levels, enabled by default\nlevels: {\n  info: Boolean,\n  debug: Boolean,\n  warning: Boolean,\n  error: Boolean,\n}\n\n// console log levels, enabled by default\nconsole: {\n  info: Boolean,\n  debug: Boolean,\n  warning: Boolean,\n  error: Boolean,\n}\n```\n\nParameters are configurable passing a *partial* or *complete* config object with desired values to the `set` method:\n\n```javascript\nLoggerhead.set(\n  {\n    url: 'http://myserver.com/my-frontend-log-endpoint',\n    levels: {\n      debug: false,\n      warning: false,\n    },\n    console: {\n      info: false,\n      debug: false,\n    }\n  }\n);\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fncounter%2Floggerhead","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fncounter%2Floggerhead","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fncounter%2Floggerhead/lists"}