{"id":15711266,"url":"https://github.com/eshengsky/serverlog","last_synced_at":"2025-04-13T21:36:42.070Z","repository":{"id":34869313,"uuid":"182648052","full_name":"eshengsky/ServerLog","owner":"eshengsky","description":"A simple, practical and innovative Node.js log library that enables you to view logs in Chrome dev tools and browser Console.","archived":false,"fork":false,"pushed_at":"2022-12-09T21:39:46.000Z","size":5128,"stargazers_count":120,"open_issues_count":4,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-27T11:51:22.640Z","etag":null,"topics":["chrome-extension","log","logger","logging","node-log","nodejs"],"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/eshengsky.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":"2019-04-22T07:47:42.000Z","updated_at":"2025-02-10T23:10:38.000Z","dependencies_parsed_at":"2023-01-15T10:00:42.976Z","dependency_job_id":null,"html_url":"https://github.com/eshengsky/ServerLog","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eshengsky%2FServerLog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eshengsky%2FServerLog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eshengsky%2FServerLog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eshengsky%2FServerLog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eshengsky","download_url":"https://codeload.github.com/eshengsky/ServerLog/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248788573,"owners_count":21161725,"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":["chrome-extension","log","logger","logging","node-log","nodejs"],"created_at":"2024-10-03T21:12:20.553Z","updated_at":"2025-04-13T21:36:42.044Z","avatar_url":"https://github.com/eshengsky.png","language":"JavaScript","readme":"# ServerLog\n\n[中文文档](https://github.com/eshengsky/ServerLog/blob/master/README_zh.md)\n\nA simple, practical and innovative Node.js log library that enables you to view logs in Chrome dev tools and browser Console.\n\nServerLog contains the following features:\n\n* Friendly log output format;\n* Support `info`, `warn`, `error` 3 log levels, as simple as possible API design;\n* By registering [Express](http://expressjs.com/) middleware, can automatically attach the request URL to the end of the log;\n* The log generated by the same batch request generates a [Request ID](#request-id) to facilitate the association and filtering of the logs; \n* The accompanying Chrome extension allows you to view request related logs in Chrome dev tools and browser Console.\n\n## Preview\n\n* Print in terminal:\n\n![image](https://raw.githubusercontent.com/eshengsky/ServerLog/master/preview_terminal.png)\n\n* Print in Chrome extension:\n\n![image](https://raw.githubusercontent.com/eshengsky/ServerLog/master/chrome-extension-server-log/preview_ext_en.png)\n\n* Print in browser Console：\n\n![image](https://raw.githubusercontent.com/eshengsky/ServerLog/master/chrome-extension-server-log/preview_console.png)\n\n## Get started\n\n### Install\n\n```bash\n\u003e npm i --save serverlog-node\n```\n\n### Usage\n\n```js\nconst serverlog = require('serverlog-node');\nconst logger = serverlog.getLogger('home');\n\nlogger.info('Something to log...');\n```\n\nIf you are using [Express](http://expressjs.com/) framework, you can get another functionality by registering middleware:\n* Automatically add request ID in the request log.\n* Automatically add the current URL in the request log.\n* Send the request log to the Chrome extension, and you can view the logs in the Chrome dev tools and browser Console panel.\n\n```js\nconst express = require('express');\nconst app = express();\nconst serverlog = require('serverlog-node');\nconst logger = serverlog.getLogger();\n\napp.use(serverlog.middleware());\n\napp.use((req, res) =\u003e {\n    logger.info('Something to log within request...');\n    res.send('Hello World!');\n});\n```\n\n## Chrome extension\n\nIt's easier to view logs using the accompanying Chrome extension.\n\nhttps://github.com/eshengsky/ServerLog/tree/master/chrome-extension-server-log\n\n## Api\n\n## serverlog\n\n### config(Options)\n\nOverwrite the default settings with the incoming Options.\n\n`Options`: parameters object.\n\n```js\nserverlog.config({\n    console: {\n        colors: true,\n        depth: null,\n        appendUrl: true,\n        forceSingleLine: false\n    },\n    extension: {\n        enable: true,\n        key: 'yourownsecretkey'\n    }\n});\n```\n\nThe full parameters supported are as follows:\n\n\u003ctable\u003e\n    \u003ctr\u003e\n        \u003cth\u003eProperty name\u003c/th\u003e\n        \u003cth\u003eDescription\u003c/th\u003e\n        \u003cth\u003eType\u003c/th\u003e\n        \u003cth\u003eDefault\u003c/th\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003econsole.colors\u003c/td\u003e\n        \u003ctd\u003eEnable colors\u003c/td\u003e\n        \u003ctd\u003eboolean\u003c/td\u003e\n        \u003ctd\u003etrue\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003econsole.depth\u003c/td\u003e\n        \u003ctd\u003eThe parsing depth of the object, see here \u003ca href=\"https://nodejs.org/dist/latest-v10.x/docs/api/util.html#util_util_inspect_object_options\"\u003eutil.inspect\u003c/a\u003e\u003c/td\u003e\n        \u003ctd\u003enumber\u003c/td\u003e\n        \u003ctd\u003enull\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003econsole.appendUrl\u003c/td\u003e\n        \u003ctd\u003eAutomatically attach the current request URL to the end of the log\u003c/td\u003e\n        \u003ctd\u003eboolean\u003c/td\u003e\n        \u003ctd\u003etrue\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003econsole.forceSingleLine\u003c/td\u003e\n        \u003ctd\u003eForce each log to not wrap\u003c/td\u003e\n        \u003ctd\u003eboolean\u003c/td\u003e\n        \u003ctd\u003efalse\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003eextension.enable\u003c/td\u003e\n        \u003ctd\u003eEnable Chrome extension functions\u003c/td\u003e\n        \u003ctd\u003eboolean\u003c/td\u003e\n        \u003ctd\u003etrue\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003eextension.key\u003c/td\u003e\n        \u003ctd\u003eThe key of the current service, see here \u003ca href=\"https://github.com/eshengsky/ServerLog/tree/master/chrome-extension-server-log#secret-key\"\u003eSecret Key\u003c/a\u003e\u003c/td\u003e\n        \u003ctd\u003estring\u003c/td\u003e\n        \u003ctd\u003eyourownsecretkey\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003eextension.maxLength\u003c/td\u003e\n        \u003ctd\u003eMax length of logs, in kb units\u003c/td\u003e\n        \u003ctd\u003enumber\u003c/td\u003e\n        \u003ctd\u003e80\u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\n### middleware()\n\nRegisters log middleware that supports frameworks that are compatible with [Express](http://expressjs.com/) middleware.\n\n```js\napp.use(serverlog.middleware());\n```\n\n### getLogger(categoryName)\n\nCreate and return a [logger](https://github.com/eshengsky/ServerLog/blob/master/README_zh.md#logger) instance.\n\n`categoryName`: String, log category name, default: normal.\n\n## logger\n\n### info(arg1, arg2, ...args)\n\nLogs an info log that can be passed in to any type, any number of parameters.\n\n```js\nlogger.info('This is an info log.');\n```\n\n### warn(arg1, arg2, ...args)\n\nLogs an warning log that can be passed in to any type, any number of parameters.\n\n```js\nlet undef;\nlogger.warn('This is a warning log.', 'Take care, undef value is:', undef);\n```\n\n### error(arg1, arg2, ...args)\n\nLogs an error log that can be passed in to any type, any number of parameters.\n\n```js\ntry {\n    foo.bar();\n} catch (err) {\n    logger.error('This is an error log. Error:', err);\n}\n```\n\n### infoC(arg1, arg2, ...args), warnC(arg1, arg2, ...args), errorC(arg1, arg2, ...args)\n\nOutput only the corresponding logs to the terminal.\n\n### infoE(arg1, arg2, ...args), warnE(arg1, arg2, ...args), errorE(arg1, arg2, ...args)\n\nOutput only the corresponding logs to the Chrome extension.\n\n```js\n// Print the full file content in the Chrome extension\nlogger.infoE('read data from local file:', JSON.parse(data));\n\n// Just print a little in console\nlogger.infoC('read data from local file, please view data in Chrome extension.');\n```\n\n## Request ID\n\n### What is request ID\n\nAfter registered middleware, all the logs associated with the request contain a request ID by default. In the same request, the request ID for all logs must be the same, and the request ID must be different in different requests.\n\nFor example, when user A accesses the index.html process ServerLog prints 10 logs, the request ID for the 10 logs are the same, and user B also accesses the page, resulting in 10 logs, which must also have the same request ID, but different from the request ID of User A.\n\n### Main role\n\nLets you be able to correlate all the logs you need in a large number of logs, as long as you know the request ID of one log.\n\n### How to find request ID\n\nWhen you can navigate to a log related to a request, between the category name of log and the contents of the log, is the request ID. For example, the following `Ra8dx5lAL`:\n\n```\n[2019-05-08 15:23:06.911] [INFO] home - {Ra8dx5lAL} This is an info log. (URL: http://localhost:3000/)\n```\n\nIf you can listen to a network request for a page, the response header `x-request-id` is the request ID:\n\n```\nx-request-id: Ra8dx5lAL\n```\n\n## Persistence\n\nServerLog only output to [stdout](https://nodejs.org/dist/latest-v10.x/docs/api/process.html#process_process_stdout) and [stderr](https://nodejs.org/dist/latest-v10.x/docs/api/process.html#process_process_stderr), does not provide the ability to log persistence, but you can do so in other ways:\n\n* Use PM2\n\nIf you deploy your project with [PM2](https://pm2.io/), logs will automatically save to the disk file. You can use PM2's own [log management](https://pm2.io/doc/en/runtime/guide/log-management/) function to achieve log viewing, rotate, and so on.\n\nBy default, log file saved into `$HOME/.pm2/logs` directory.\n\n* Common\n\nIn a Linux environment, it is easy to redirect the output to a file with very simple commands, such as:\n\n```bash\nnode server.js \u003e logfile.txt\n```\n\nSpecific can be referred [here](https://askubuntu.com/questions/420981/how-do-i-save-terminal-output-to-a-file).\n\n## Example\nhttps://github.com/eshengsky/ServerLog/tree/master/example\n\n## License\nMIT License\n\nCopyright (c) 2019 Sky.Sun\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feshengsky%2Fserverlog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feshengsky%2Fserverlog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feshengsky%2Fserverlog/lists"}