{"id":13508353,"url":"https://github.com/megahertz/electron-log","last_synced_at":"2025-05-12T13:19:14.167Z","repository":{"id":40798457,"uuid":"57876224","full_name":"megahertz/electron-log","owner":"megahertz","description":"Simple logging module Electron/Node.js/NW.js application. No dependencies. No complicated configuration.","archived":false,"fork":false,"pushed_at":"2025-05-01T16:15:32.000Z","size":611,"stargazers_count":1382,"open_issues_count":17,"forks_count":133,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-05-12T13:18:57.956Z","etag":null,"topics":["console","electron","file","log","logging","nwjs"],"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/megahertz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2016-05-02T08:54:04.000Z","updated_at":"2025-05-11T13:20:29.000Z","dependencies_parsed_at":"2023-02-16T20:00:38.075Z","dependency_job_id":"4bbe6919-0ee2-424e-a039-8b1590e6705a","html_url":"https://github.com/megahertz/electron-log","commit_stats":{"total_commits":495,"total_committers":31,"mean_commits":15.96774193548387,"dds":0.09696969696969693,"last_synced_commit":"ecb92d8af8cff523dedf8bf39c0b07aaed742df0"},"previous_names":[],"tags_count":93,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megahertz%2Felectron-log","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megahertz%2Felectron-log/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megahertz%2Felectron-log/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megahertz%2Felectron-log/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/megahertz","download_url":"https://codeload.github.com/megahertz/electron-log/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253745196,"owners_count":21957319,"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":["console","electron","file","log","logging","nwjs"],"created_at":"2024-08-01T02:00:51.871Z","updated_at":"2025-05-12T13:19:14.089Z","avatar_url":"https://github.com/megahertz.png","language":"JavaScript","readme":"# electron-log\n[![Tests](https://github.com/megahertz/electron-log/actions/workflows/tests.yml/badge.svg)](https://github.com/megahertz/electron-log/actions/workflows/tests.yml)\n[![NPM version](https://badge.fury.io/js/electron-log.svg)](https://badge.fury.io/js/electron-log)\n[![Downloads](https://img.shields.io/npm/dw/electron-log)](https://img.shields.io/npm/dw/electron-log)\n\nSimple logging module Electron/Node.js/NW.js application.\nNo dependencies. No complicated configuration.\n\nBy default, it writes logs to the following locations:\n\n - **on Linux:** `~/.config/{app name}/logs/main.log`\n - **on macOS:** `~/Library/Logs/{app name}/main.log`\n - **on Windows:** `%USERPROFILE%\\AppData\\Roaming\\{app name}\\logs\\main.log`\n\n## Installation\n\nStarts from v5, electron-log requires Electron 13+ or\nNode.js 14+. Feel free to use electron-log v4 for older runtime. v4\nsupports Node.js 0.10+ and almost any Electron build.\n\nInstall with [npm](https://npmjs.org/package/electron-log):\n\n    npm install electron-log\n    \n## Usage\n\n### Main process\n\n```js\nimport log from 'electron-log/main';\n\n// Optional, initialize the logger for any renderer process\nlog.initialize();\n\nlog.info('Log from the main process');\n```\n\n### Renderer process\n\nIf a bundler is used, you can just import the module:\n\n```typescript\nimport log from 'electron-log/renderer';\nlog.info('Log from the renderer process');\n```\n\nThis function uses sessions to inject a preload script to make the logger\navailable in a renderer process.\n\nWithout a bundler, you can use a global variable `__electronLog`. It contains\nonly log functions like `info`, `warn` and so on.\n\nThere are a few other ways how a logger can be initialized for a renderer\nprocess. [Read more](docs/initialize.md).\n\n### Preload script\n\nTo use the logger inside a preload script, use the \n`electron-log/renderer` import. \nThere's also the `electron-log/preload` entrypoint, but it's used only as a \nbridge between the main and renderer processes and doesn't export a logger. In\nmost cases, you don't need this preload entrypoint.\n\n### Node.js and NW.js\n\n```typescript\nimport log from 'electron-log/node';\nlog.info('Log from the nw.js or node.js');\n```\n\n### electron-log v2.x, v3.x, v4.x\n\nIf you would like to upgrade to the latest version, read\n[the migration guide](docs/migration.md) and [the changelog](CHANGELOG.md).\n\n### Log levels\n\nelectron-log supports the following log levels:\n\n    error, warn, info, verbose, debug, silly\n\n### Transport\n\nTransport is a simple function which does some work with log message.\nBy default, two transports are active: console and file.\n\nYou can set transport options or use methods using:\n\n`log.transports.console.format = '{h}:{i}:{s} {text}';`\n\n`log.transports.file.getFile();`\n\nEach transport has `level` and \n[`transforms`](docs/extend.md#transforms) options.\n\n#### Console transport\n\nJust prints a log message to application console (main process) or to\nDevTools console (renderer process).\n\n##### Options\n\n - **[format](docs/transports/format.md)**, default\n   `'%c{h}:{i}:{s}.{ms}%c › {text}'` (main),\n   `'{h}:{i}:{s}.{ms} › {text}'` (renderer)\n - **level**, default 'silly'\n - **useStyles**, force enable/disable styles\n\n[Read more about console transport](docs/transports/console.md).\n\n#### File transport\n\nThe file transport writes log messages to a file.\n\n##### Options\n\n - **[format](docs/transports/format.md)**, default\n   `'[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}] {text}'`\n - **level**, default 'silly'\n - **resolvePathFn** function sets the log path, for example\n \n```js\nlog.transports.file.resolvePathFn = () =\u003e path.join(APP_DATA, 'logs/main.log');\n```\n\n[Read more about file transport](docs/transports/file.md).\n\n#### IPC transport\nIt displays log messages from main process in the renderer's DevTools console.\nBy default, it's disabled for a production build. You can enable in the\nproduction mode by setting the `level` property.\n\n\n##### Options\n\n - **level**, default 'silly' in the dev mode, `false` in the production.\n\n#### Remote transport\n\nSends a JSON POST request with `LogMessage` in the body to the specified url.\n\n##### Options\n\n - **level**, default false\n - **url**, remote endpoint\n\n[Read more about remote transport](docs/transports/remote.md).\n\n#### Disable a transport\n\nJust set level property to false, for example:\n\n```js\nlog.transports.file.level = false;\nlog.transports.console.level = false;\n```\n\n#### [Override/add a custom transport](docs/extend.md#transport)\n\nTransport is just a function `(msg: LogMessage) =\u003e void`, so you can\neasily override/add your own transport.\n[More info](docs/extend.md#transport).\n\n#### Third-party transports\n\n- [Datadog](https://github.com/theogravity/electron-log-transport-datadog)\n\n### Overriding console.log\n\nSometimes it's helpful to use electron-log instead of default `console`. It's\npretty easy:\n\n```js\nconsole.log = log.log;\n```\n\nIf you would like to override other functions like `error`, `warn` and so on:\n\n```js\nObject.assign(console, log.functions);\n```\n\n### Colors\n\nColors can be used for both main and DevTools console.\n\n`log.info('%cRed text. %cGreen text', 'color: red', 'color: green')`\n\nAvailable colors:\n - unset (reset to default color)\n - black\n - red\n - green\n - yellow\n - blue\n - magenta\n - cyan\n - white\n \nFor DevTools console you can use other CSS properties.\n\n### [Catch errors](docs/errors.md)\n\nelectron-log can catch and log unhandled errors/rejected promises:\n\n`log.errorHandler.startCatching(options?)`;\n\n[More info](docs/errors.md).\n\n#### Electron events logging\n\nSometimes it's helpful to save critical electron events to the log file.\n\n`log.eventLogger.startLogging(options?)`;\n\nBy default, it save the following events:\n - `certificate-error`, `child-process-gone`, `render-process-gone` of `app`\n - `crashed`, `gpu-process-crashed` of `webContents`\n - `did-fail-load`, `did-fail-provisional-load`, `plugin-crashed`,\n   `preload-error` of every WebContents. You can switch any event on/off.\n\n[More info](docs/events.md).\n\n### [Hooks](docs/extend.md#hooks)\n\nIn some situations, you may want to get more control over logging. Hook\nis a function which is called on each transport call.\n\n`(message: LogMessage, transport: Transport, transportName) =\u003e LogMessage`\n\n[More info](docs/extend.md#hooks).\n\n### Multiple logger instances\n\nYou can create multiple logger instances with different settings:\n\n```js\nimport log from 'electron-log/main';\n\nconst anotherLogger = log.create({ logId: 'anotherInstance' });\n```\n\nBe aware that you need to configure each instance (e.g. log file path) \nseparately.\n\n### Logging scopes\n\n```js\nimport log from 'electron-log/main';\nconst userLog = log.scope('user');\n\nuserLog.info('message with user scope');\n// Prints 12:12:21.962 (user) › message with user scope\n```\n\nBy default, scope labels are padded in logs. To disable it, set  \n`log.scope.labelPadding = false`.\n\n### Buffering\n\nIt's like a transaction, you may add some logs to the buffer and then decide \nwhether to write these logs or not. It allows adding verbose logs only\nwhen some operations failed.\n\n```js\nimport log from 'electron-log/main';\n\nlog.buffering.begin();\ntry {\n  log.info('First silly message');\n  // do somethings complex\n  log.info('Second silly message');\n  // do something else\n   \n  // Finished fine, we don't need these logs anymore\n  log.buffering.reject();\n} catch (e) {\n  log.buffering.commit();\n  log.warn(e);\n}\n```\n\n## Related\n\n - [electron-cfg](https://github.com/megahertz/electron-cfg) -\n   Settings for your Electron application.\n","funding_links":[],"categories":["JavaScript","Library","Tools","三、开发者必备工具"],"sub_categories":["Logging","For Electron","2. 开发调试辅助"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmegahertz%2Felectron-log","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmegahertz%2Felectron-log","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmegahertz%2Felectron-log/lists"}