{"id":20192153,"url":"https://github.com/rocicorp/logger","last_synced_at":"2025-04-10T09:52:35.041Z","repository":{"id":42519525,"uuid":"471027670","full_name":"rocicorp/logger","owner":"rocicorp","description":"Context-carrying logger with conditional methods.","archived":false,"fork":false,"pushed_at":"2024-12-09T22:07:38.000Z","size":87,"stargazers_count":13,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-01T02:03:40.943Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/@rocicorp/logger","language":"TypeScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rocicorp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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}},"created_at":"2022-03-17T14:49:12.000Z","updated_at":"2024-12-26T03:23:47.000Z","dependencies_parsed_at":"2025-02-27T20:16:59.365Z","dependency_job_id":"a998154f-80ca-4aea-91fd-e53e5d70a947","html_url":"https://github.com/rocicorp/logger","commit_stats":{"total_commits":29,"total_committers":7,"mean_commits":4.142857142857143,"dds":0.5517241379310345,"last_synced_commit":"9b8e1a14e2b38fd74594ff7e4031ffa40e123a43"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocicorp%2Flogger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocicorp%2Flogger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocicorp%2Flogger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocicorp%2Flogger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rocicorp","download_url":"https://codeload.github.com/rocicorp/logger/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248198183,"owners_count":21063626,"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-11-14T04:00:46.245Z","updated_at":"2025-04-10T09:52:35.014Z","avatar_url":"https://github.com/rocicorp.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# logger\n\nProvides a simple logging interface as well as a `LogContext` class which\ncarries a context around.\n\n# Installation\n\n```\nnpm install @rocicorp/logger\n```\n\n# Usage\n\n## LogContext\n\n`LogContext` includes a context that gets included in the log message.\n\n```js\nimport {LogContext} from '@rocicorp/logger';\n\nconst lc = new LogContext('info');\nlc.info('hello'); // prints \"hello\"\n\nconst lc2 = new LogContext('info', {name: 'alice'});\nlc.info('hello'); // prints \"name=alice hello\"\n\nconst lc3 = lc2.withContext('bbb');\nlc3.info('hello'); // prints \"name=alice bbb hello\"\n\nconst lc4 = lc3.withContext('ccc');\nlc4.info('hello'); // prints \"name=alice bbb ccc hello\"\n\nconst lc5 = lc4.withContext('ddd', 'eee');\nlc5.info('hello'); // prints \"name=alice bbb ccc ddd=eee hello\"\n\n// Or get a context logger appropriate for the Node environment.\nconst nlc = newNodeLogContext('debug');\nnlc.info('fff'); // prints \"INF fff\"\n```\n\n## OptionalLogger\n\n```ts\ninterface OptionalLogger { ... }\n```\n\nThis interface is used to provide conditional logging. It is intended to be used\nwith conditional method calling `?.()`.\n\n```ts\nimport {ConsoleLogger, type OptionalLogger} from '@rocicorp/logger';\n\nconst l: OptionalLogger = new ConsoleLogger('info');\nl.info?.('hello'); // prints \"hello\"\nl.debug?.('hello'); // does not print anything\n```\n\nBut more importantly it does not evaluate the arguments.\n\n```ts\nimport {ConsoleLogger, type OptionalLogger} from '@rocicorp/logger';\nconst alwaysThrows = () =\u003e {\n  throw new Error();\n};\nconst l: OptionalLogger = new ConsoleLogger('info');\nl.debug?.(alwaysThrows()); // does not print anything, but does not throw\n```\n\n# Implementing Your Own `LogSink`\n\nThere is also the `LogSink` interface which is very minimal. Its intended use is\nfor custom log implementations.\n\n```ts\nclass ExampleLogSink implements LogSink {\n  log(level: LogLevel, ...args: unknown[]): void {\n    //...\n  }\n}\n```\n\nYou can get an `OptionalLogger` from a `LogSink` using `OptionalLoggerImpl`.\n\n```ts\nimport {\n  OptionalLoggerImpl,\n  type LogSink,\n  type OptionalLogger,\n} from '@rocicorp/logger';\n\nconst logSink: LogSink = new ExampleLogSink();\nconst l: OptionalLogger = new OptionalLoggerImpl(logSink);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frocicorp%2Flogger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frocicorp%2Flogger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frocicorp%2Flogger/lists"}