From 1c71a59a89a421bd73b6980847403eba45a91a7a Mon Sep 17 00:00:00 2001 From: Daniel Freytag Date: Fri, 22 Nov 2024 09:01:33 +0100 Subject: [PATCH] feat: add logger as initial jsr test --- .github/workflows/publish.yml | 35 +++++ LICENSE | 37 ++--- README.md | 10 +- biome.json | 43 ++++++ deno.jsonc | 35 +++++ deno.lock | 247 ++++++++++++++++++++++++++++++++++ logger/README.md | 8 ++ logger/deno.jsonc | 9 ++ logger/logger.ts | 60 +++++++++ 9 files changed, 466 insertions(+), 18 deletions(-) create mode 100644 .github/workflows/publish.yml create mode 100644 biome.json create mode 100644 deno.jsonc create mode 100644 deno.lock create mode 100644 logger/README.md create mode 100644 logger/deno.jsonc create mode 100644 logger/logger.ts diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..0f14245 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,35 @@ +# adapted from https://github.com/denoland/std/blob/main/.github/workflows/workspace_publish.yml + +name: workspace publish + +on: + release: + types: [published] + workflow_dispatch: + +jobs: + publish: + runs-on: ubuntu-latest + timeout-minutes: 30 + + permissions: + contents: read + id-token: write + + steps: + - name: Clone repository + uses: actions/checkout@v4 + + - name: Set up Deno + uses: denoland/setup-deno@v2 + with: + deno-version: canary + + - name: Format + run: deno fmt --check + + # - name: Test + # run: deno task test + + - name: Publish to JSR + run: deno publish diff --git a/LICENSE b/LICENSE index 245d61b..fdddb29 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,24 @@ -MIT License +This is free and unencumbered software released into the public domain. -Copyright (c) 2024 Dan +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +For more information, please refer to diff --git a/README.md b/README.md index a861934..9e449fe 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -# js-utils \ No newline at end of file +# js-utils + +A collection of utilities for TypeScript and JavaScript. + +This repository is work in progress. + +## License + +[Unlicense](./LICENSE) - also see [unlicense.org](https://unlicense.org) diff --git a/biome.json b/biome.json new file mode 100644 index 0000000..d34fd9c --- /dev/null +++ b/biome.json @@ -0,0 +1,43 @@ +{ + "$schema": "https://biomejs.dev/schemas/1.9.3/schema.json", + "files": { + "maxSize": 2097152, + "ignore": ["*.vue"] + }, + "formatter": { + "indentStyle": "tab", + "formatWithErrors": true, + "lineWidth": 120 + }, + "javascript": { + "formatter": { + "quoteStyle": "single", + "semicolons": "asNeeded", + "trailingCommas": "all" + }, + "globals": ["cast", "Bun"] + }, + "linter": { + "enabled": true, + "rules": { + "recommended": true, + "correctness": { + "all": true, + "noNodejsModules": "off", + "noUndeclaredDependencies": "info" + }, + "performance": { + "all": true + }, + "security": { + "all": true + }, + "nursery": { + "useSortedClasses": "info" + } + } + }, + "organizeImports": { + "enabled": true + } +} diff --git a/deno.jsonc b/deno.jsonc new file mode 100644 index 0000000..75a0410 --- /dev/null +++ b/deno.jsonc @@ -0,0 +1,35 @@ +{ + "compilerOptions": { + "strict": true, + "exactOptionalPropertyTypes": true, + "useUnknownInCatchVariables": true, + "noImplicitOverride": true, + "noUncheckedIndexedAccess": true + }, + "exclude": [".git"], + "fmt": { + "useTabs": true, + "lineWidth": 120, + "semiColons": false, + "singleQuote": true + }, + "tasks": { + "check": "deno fmt --check" + }, + "lint": { + "rules": { + "include": [ + "camelcase", + "no-sync-fn-in-async-fn", + "single-var-declarator", + "verbatim-module-syntax", + "no-console" + ] + } + }, + "workspace": ["./logger"], + "imports": { + "@biomejs/biome": "npm:@biomejs/biome@^1.9.4", + "@types/node": "npm:@types/node@^22.9.0" + } +} diff --git a/deno.lock b/deno.lock new file mode 100644 index 0000000..f68f84d --- /dev/null +++ b/deno.lock @@ -0,0 +1,247 @@ +{ + "version": "4", + "specifiers": { + "npm:@biomejs/biome@^1.9.4": "1.9.4", + "npm:@types/node@*": "22.5.4", + "npm:@types/node@^22.9.0": "22.9.0", + "npm:winston@^3.17.0": "3.17.0" + }, + "npm": { + "@biomejs/biome@1.9.4": { + "integrity": "sha512-1rkd7G70+o9KkTn5KLmDYXihGoTaIGO9PIIN2ZB7UJxFrWw04CZHPYiMRjYsaDvVV7hP1dYNRLxSANLaBFGpog==", + "dependencies": [ + "@biomejs/cli-darwin-arm64", + "@biomejs/cli-darwin-x64", + "@biomejs/cli-linux-arm64", + "@biomejs/cli-linux-arm64-musl", + "@biomejs/cli-linux-x64", + "@biomejs/cli-linux-x64-musl", + "@biomejs/cli-win32-arm64", + "@biomejs/cli-win32-x64" + ] + }, + "@biomejs/cli-darwin-arm64@1.9.4": { + "integrity": "sha512-bFBsPWrNvkdKrNCYeAp+xo2HecOGPAy9WyNyB/jKnnedgzl4W4Hb9ZMzYNbf8dMCGmUdSavlYHiR01QaYR58cw==" + }, + "@biomejs/cli-darwin-x64@1.9.4": { + "integrity": "sha512-ngYBh/+bEedqkSevPVhLP4QfVPCpb+4BBe2p7Xs32dBgs7rh9nY2AIYUL6BgLw1JVXV8GlpKmb/hNiuIxfPfZg==" + }, + "@biomejs/cli-linux-arm64-musl@1.9.4": { + "integrity": "sha512-v665Ct9WCRjGa8+kTr0CzApU0+XXtRgwmzIf1SeKSGAv+2scAlW6JR5PMFo6FzqqZ64Po79cKODKf3/AAmECqA==" + }, + "@biomejs/cli-linux-arm64@1.9.4": { + "integrity": "sha512-fJIW0+LYujdjUgJJuwesP4EjIBl/N/TcOX3IvIHJQNsAqvV2CHIogsmA94BPG6jZATS4Hi+xv4SkBBQSt1N4/g==" + }, + "@biomejs/cli-linux-x64-musl@1.9.4": { + "integrity": "sha512-gEhi/jSBhZ2m6wjV530Yy8+fNqG8PAinM3oV7CyO+6c3CEh16Eizm21uHVsyVBEB6RIM8JHIl6AGYCv6Q6Q9Tg==" + }, + "@biomejs/cli-linux-x64@1.9.4": { + "integrity": "sha512-lRCJv/Vi3Vlwmbd6K+oQ0KhLHMAysN8lXoCI7XeHlxaajk06u7G+UsFSO01NAs5iYuWKmVZjmiOzJ0OJmGsMwg==" + }, + "@biomejs/cli-win32-arm64@1.9.4": { + "integrity": "sha512-tlbhLk+WXZmgwoIKwHIHEBZUwxml7bRJgk0X2sPyNR3S93cdRq6XulAZRQJ17FYGGzWne0fgrXBKpl7l4M87Hg==" + }, + "@biomejs/cli-win32-x64@1.9.4": { + "integrity": "sha512-8Y5wMhVIPaWe6jw2H+KlEm4wP/f7EW3810ZLmDlrEEy5KvBsb9ECEfu/kMWD484ijfQ8+nIi0giMgu9g1UAuuA==" + }, + "@colors/colors@1.6.0": { + "integrity": "sha512-Ir+AOibqzrIsL6ajt3Rz3LskB7OiMVHqltZmspbW/TJuTVuyOMirVqAkjfY6JISiLHgyNqicAC8AyHHGzNd/dA==" + }, + "@dabh/diagnostics@2.0.3": { + "integrity": "sha512-hrlQOIi7hAfzsMqlGSFyVucrx38O+j6wiGOf//H2ecvIEqYN4ADBSS2iLMh5UFyDunCNniUIPk/q3riFv45xRA==", + "dependencies": [ + "colorspace", + "enabled", + "kuler" + ] + }, + "@types/node@22.5.4": { + "integrity": "sha512-FDuKUJQm/ju9fT/SeX/6+gBzoPzlVCzfzmGkwKvRHQVxi4BntVbyIwf6a4Xn62mrvndLiml6z/UBXIdEVjQLXg==", + "dependencies": [ + "undici-types" + ] + }, + "@types/node@22.9.0": { + "integrity": "sha512-vuyHg81vvWA1Z1ELfvLko2c8f34gyA0zaic0+Rllc5lbCnbSyuvb2Oxpm6TAUAC/2xZN3QGqxBNggD1nNR2AfQ==", + "dependencies": [ + "undici-types" + ] + }, + "@types/triple-beam@1.3.5": { + "integrity": "sha512-6WaYesThRMCl19iryMYP7/x2OVgCtbIVflDGFpWnb9irXI3UjYE4AzmYuiUKY1AJstGijoY+MgUszMgRxIYTYw==" + }, + "async@3.2.6": { + "integrity": "sha512-htCUDlxyyCLMgaM3xXg0C0LW2xqfuQ6p05pCEIsXuyQ+a1koYKTuBMzRNwmybfLgvJDMd0r1LTn4+E0Ti6C2AA==" + }, + "color-convert@1.9.3": { + "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "dependencies": [ + "color-name" + ] + }, + "color-name@1.1.3": { + "integrity": "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw==" + }, + "color-string@1.9.1": { + "integrity": "sha512-shrVawQFojnZv6xM40anx4CkoDP+fZsw/ZerEMsW/pyzsRbElpsL/DBVW7q3ExxwusdNXI3lXpuhEZkzs8p5Eg==", + "dependencies": [ + "color-name", + "simple-swizzle" + ] + }, + "color@3.2.1": { + "integrity": "sha512-aBl7dZI9ENN6fUGC7mWpMTPNHmWUSNan9tuWN6ahh5ZLNk9baLJOnSMlrQkHcrfFgz2/RigjUVAjdx36VcemKA==", + "dependencies": [ + "color-convert", + "color-string" + ] + }, + "colorspace@1.1.4": { + "integrity": "sha512-BgvKJiuVu1igBUF2kEjRCZXol6wiiGbY5ipL/oVPwm0BL9sIpMIzM8IK7vwuxIIzOXMV3Ey5w+vxhm0rR/TN8w==", + "dependencies": [ + "color", + "text-hex" + ] + }, + "enabled@2.0.0": { + "integrity": "sha512-AKrN98kuwOzMIdAizXGI86UFBoo26CL21UM763y1h/GMSJ4/OHU9k2YlsmBpyScFo/wbLzWQJBMCW4+IO3/+OQ==" + }, + "fecha@4.2.3": { + "integrity": "sha512-OP2IUU6HeYKJi3i0z4A19kHMQoLVs4Hc+DPqqxI2h/DPZHTm/vjsfC6P0b4jCMy14XizLBqvndQ+UilD7707Jw==" + }, + "fn.name@1.1.0": { + "integrity": "sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==" + }, + "inherits@2.0.4": { + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "is-arrayish@0.3.2": { + "integrity": "sha512-eVRqCvVlZbuw3GrM63ovNSNAeA1K16kaR/LRY/92w0zxQ5/1YzwblUX652i4Xs9RwAGjW9d9y6X88t8OaAJfWQ==" + }, + "is-stream@2.0.1": { + "integrity": "sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg==" + }, + "kuler@2.0.0": { + "integrity": "sha512-Xq9nH7KlWZmXAtodXDDRE7vs6DU1gTU8zYDHDiWLSip45Egwq3plLHzPn27NgvzL2r1LMPC1vdqh98sQxtqj4A==" + }, + "logform@2.7.0": { + "integrity": "sha512-TFYA4jnP7PVbmlBIfhlSe+WKxs9dklXMTEGcBCIvLhE/Tn3H6Gk1norupVW7m5Cnd4bLcr08AytbyV/xj7f/kQ==", + "dependencies": [ + "@colors/colors", + "@types/triple-beam", + "fecha", + "ms", + "safe-stable-stringify", + "triple-beam" + ] + }, + "ms@2.1.3": { + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "one-time@1.0.0": { + "integrity": "sha512-5DXOiRKwuSEcQ/l0kGCF6Q3jcADFv5tSmRaJck/OqkVFcOzutB134KRSfF0xDrL39MNnqxbHBbUUcjZIhTgb2g==", + "dependencies": [ + "fn.name" + ] + }, + "readable-stream@3.6.2": { + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "dependencies": [ + "inherits", + "string_decoder", + "util-deprecate" + ] + }, + "safe-buffer@5.2.1": { + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" + }, + "safe-stable-stringify@2.5.0": { + "integrity": "sha512-b3rppTKm9T+PsVCBEOUR46GWI7fdOs00VKZ1+9c1EWDaDMvjQc6tUwuFyIprgGgTcWoVHSKrU8H31ZHA2e0RHA==" + }, + "simple-swizzle@0.2.2": { + "integrity": "sha512-JA//kQgZtbuY83m+xT+tXJkmJncGMTFT+C+g2h2R9uxkYIrE2yy9sgmcLhCnw57/WSD+Eh3J97FPEDFnbXnDUg==", + "dependencies": [ + "is-arrayish" + ] + }, + "stack-trace@0.0.10": { + "integrity": "sha512-KGzahc7puUKkzyMt+IqAep+TVNbKP+k2Lmwhub39m1AsTSkaDutx56aDCo+HLDzf/D26BIHTJWNiTG1KAJiQCg==" + }, + "string_decoder@1.3.0": { + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dependencies": [ + "safe-buffer" + ] + }, + "text-hex@1.0.0": { + "integrity": "sha512-uuVGNWzgJ4yhRaNSiubPY7OjISw4sw4E5Uv0wbjp+OzcbmVU/rsT8ujgcXJhn9ypzsgr5vlzpPqP+MBBKcGvbg==" + }, + "triple-beam@1.4.1": { + "integrity": "sha512-aZbgViZrg1QNcG+LULa7nhZpJTZSLm/mXnHXnbAbjmN5aSa0y7V+wvv6+4WaBtpISJzThKy+PIPxc1Nq1EJ9mg==" + }, + "undici-types@6.19.8": { + "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==" + }, + "util-deprecate@1.0.2": { + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" + }, + "winston-transport@4.9.0": { + "integrity": "sha512-8drMJ4rkgaPo1Me4zD/3WLfI/zPdA9o2IipKODunnGDcuqbHwjsbB79ylv04LCGGzU0xQ6vTznOMpQGaLhhm6A==", + "dependencies": [ + "logform", + "readable-stream", + "triple-beam" + ] + }, + "winston@3.17.0": { + "integrity": "sha512-DLiFIXYC5fMPxaRg832S6F5mJYvePtmO5G9v9IgUFPhXm9/GkXarH/TUrBAVzhTCzAj9anE/+GjrgXp/54nOgw==", + "dependencies": [ + "@colors/colors", + "@dabh/diagnostics", + "async", + "is-stream", + "logform", + "one-time", + "readable-stream", + "safe-stable-stringify", + "stack-trace", + "triple-beam", + "winston-transport" + ] + } + }, + "remote": { + "https://esm.sh/@bunny.net/edgescript-sdk@0.11.2": "7732aa338bf5f0c7d13e294c15833416d4bad59807de874c59f36c1f81acd935", + "https://esm.sh/jsr/@hono/hono@4.6.10": "cf6e6ff70376b2ed1dc2c03b0ea0e0ddb94a750b9924731c8c1f355d028cffb2", + "https://esm.sh/jsr/@hono/hono@4.6.10/cors": "dc14db48bfb6d71008ebfd7c8eb51aa46b7b3e0f505348fca3693d3fdd32abe1", + "https://esm.sh/jsr/@hono/hono@4.6.10/etag": "c55cd5ff11fe9c8c16e49fc94064a4c6d949f6b0f9bc1731fb4498765ed0a8a2", + "https://esm.sh/jsr/@hono/hono@4.6.10/pretty-json": "9dc73628b94c63f8bcf804e1881939ede133f98a57913f46759159a60e54c464", + "https://esm.sh/v135/@bunny.net/edgescript-sdk@0.11.2/denonext/edgescript-sdk.mjs": "e8171a66904061537db78a8c5fe20a8e47788056fa85d99dfdd1fb7167f89a70", + "https://esm.sh/v135/@jsr/hono__hono@4.6.10/denonext/cors.js": "fbc5b1adc0d2c86c31cd3d20182bc1faf313cddae89e2bb1da3b61afe09b9bd9", + "https://esm.sh/v135/@jsr/hono__hono@4.6.10/denonext/etag.js": "f39b885b71ea152b7a2ea2aa5a2201abd8301e6be1d294f10822c2f70b80f9cf", + "https://esm.sh/v135/@jsr/hono__hono@4.6.10/denonext/hono-base.js": "2ad8ffc6f9e52c3d0f300a9633ac0359fb16b474fa0cc46f5d99b549b809271b", + "https://esm.sh/v135/@jsr/hono__hono@4.6.10/denonext/hono__hono.mjs": "c1bea804a3fec7e885ea3ea3bfeac86eca252b816d4ac70c53d9099fc51026c6", + "https://esm.sh/v135/@jsr/hono__hono@4.6.10/denonext/pretty-json.js": "dc4b6e51d5f5d7eb225915f39e2036d2a2b5d2b0ca4a19d2b57ec5166c14e5d0", + "https://esm.sh/v135/@jsr/hono__hono@4.6.10/denonext/request.js": "2527a9eaccc9507e22b4957f7e0aa9693a06ffe1742a5f1d879b934749fae51c", + "https://esm.sh/v135/@jsr/hono__hono@4.6.10/denonext/router.js": "6a68f950b009de40bf885125b69c7411244d6d1a17f232cf5eb656b36edbb12f", + "https://esm.sh/v135/@jsr/hono__hono@4.6.10/denonext/router/reg-exp-router.js": "46e2bb114d1e845d0dab41a5e291f474155c5bed77845b158ff1f8a83a6b01fc", + "https://esm.sh/v135/@jsr/hono__hono@4.6.10/denonext/router/smart-router.js": "0f6f98361f32f17caf439c8b4824f6856ca5f31a3f20e615a78fb1a97503a107", + "https://esm.sh/v135/@jsr/hono__hono@4.6.10/denonext/router/trie-router.js": "64b9cbb405cac8fee816a37cd21b0536da1701764a6122dc885b81858166b443", + "https://esm.sh/v135/@jsr/hono__hono@4.6.10/denonext/utils/body.js": "2698eef74153030f395ca86560d5af63786d103af17d82eae23ccf6ae3b43fab", + "https://esm.sh/v135/@jsr/hono__hono@4.6.10/denonext/utils/html.js": "cef8ce2d9fc48aabf06c0935eb7e4a62d117c278bf084686113bd6cde835a6e6", + "https://esm.sh/v135/@jsr/hono__hono@4.6.10/denonext/utils/url.js": "0f5d76b7f749814518f38f3dfa29cd6f7cfadb4d263271ef944e474356104b1c" + }, + "workspace": { + "dependencies": [ + "npm:@biomejs/biome@^1.9.4", + "npm:@types/node@^22.9.0" + ], + "members": { + "logger": { + "dependencies": [ + "npm:winston@^3.17.0" + ] + } + } + } +} diff --git a/logger/README.md b/logger/README.md new file mode 100644 index 0000000..c011ed7 --- /dev/null +++ b/logger/README.md @@ -0,0 +1,8 @@ +# `@frytg/logger` - Winston wrapper + +This is a simple wrapper around Winston to provide a logger that is easy to use and configure. + +It accesses and inject several env variables (like runtime or package version) to each log event. + +If `IS_LOCAL` is set to `true`, it will use a more human readable, colorized output format, otherwise it will use JSON +on one line. diff --git a/logger/deno.jsonc b/logger/deno.jsonc new file mode 100644 index 0000000..87003b7 --- /dev/null +++ b/logger/deno.jsonc @@ -0,0 +1,9 @@ +{ + "$schema": "https://jsr.io/schema/config-file.v1.json", + "name": "@frytg/logger", + "version": "0.0.1", + "exports": "./logger.ts", + "imports": { + "winston": "npm:winston@^3.17.0" + } +} diff --git a/logger/logger.ts b/logger/logger.ts new file mode 100644 index 0000000..76a72bf --- /dev/null +++ b/logger/logger.ts @@ -0,0 +1,60 @@ +// load packages +import os from 'node:os' +import process from 'node:process' +import type { Logform, Logger } from 'winston' +import { config, createLogger, format, transports } from 'winston' + +// set config once +const hostName = os.hostname() + +// Format error objects +const convertError = format((event) => { + if (event?.error instanceof Error) { + event.error = { + code: event.error.code, + message: event.error.message, + stack: event.error.stack, + } + } + return event +}) + +// Detect the process version +const detectProcessVersion = () => { + if (process.versions?.bun) return `bun-${process.versions.bun}` + if (process.versions?.deno) return `deno-${process.versions.deno}` + return process.version +} + +// Add global context to log events +const convertGlobals = format((event) => { + event.host = process.env.K_REVISION || hostName + event.serviceName = process.env.SERVICE_NAME + event.stage = process.env.STAGE + event.version = process.env.npm_package_version + event.runtime = detectProcessVersion() + return event +}) + +// Set up format based on environment +let formatConfig: Logform.Format = format.combine(convertError(), convertGlobals(), format.json()) +if (process.env.IS_LOCAL === 'true') { + formatConfig = format.combine( + convertError(), + convertGlobals(), + format.timestamp(), + format.json({ space: 4 }), + format.colorize({ all: true }), + ) +} + +// Initialize logger +const logger: Logger = createLogger({ + level: process.env.STAGE === 'dev' ? 'debug' : 'info', + levels: config.syslog.levels, + exitOnError: false, + format: formatConfig, + transports: [new transports.Console()], +}) + +export default logger