mynth-logger 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request:
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ timeout-minutes: 10
13
+ steps:
14
+ - name: Checkout repository
15
+ uses: actions/checkout@v3
16
+
17
+ - name: Set up Node.js
18
+ uses: actions/setup-node@v3
19
+ with:
20
+ node-version: 18.16.1
21
+
22
+ - name: Update npm
23
+ run: npm install -g npm@9.7.2
24
+
25
+ - name: Install dependencies
26
+ run: npm ci --include dev
27
+
28
+ - name: Run tests
29
+ run: npm run test
30
+
31
+ lint:
32
+ runs-on: ubuntu-latest
33
+ timeout-minutes: 10
34
+
35
+ steps:
36
+ - name: Checkout code
37
+ uses: actions/checkout@v3
38
+
39
+ - name: Set up Node.js
40
+ uses: actions/setup-node@v3
41
+ with:
42
+ node-version: 18.16.1
43
+
44
+ - name: Install dependencies
45
+ run: npm install --include dev
46
+
47
+ - name: Lint
48
+ run: npm run lint
49
+
50
+ build:
51
+ runs-on: ubuntu-latest
52
+ timeout-minutes: 10
53
+
54
+ steps:
55
+ - name: Checkout code
56
+ uses: actions/checkout@v3
57
+
58
+ - name: Set up Node.js
59
+ uses: actions/setup-node@v3
60
+ with:
61
+ node-version: 18.16.1
62
+
63
+ - name: Install dependencies
64
+ run: npm install --include dev
65
+
66
+ - name: Validate TypeScript
67
+ run: npm run build
@@ -0,0 +1,36 @@
1
+ name: Publish to npm
2
+
3
+ on:
4
+ pull_request:
5
+ types:
6
+ - closed
7
+ branches:
8
+ - main
9
+
10
+ jobs:
11
+ publish-npm:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v3
15
+ with:
16
+ ref: ${{ github.head_ref }}
17
+ fetch-depth: 0
18
+ token: ${{ secrets.PAT }}
19
+ - uses: actions/setup-node@v3
20
+ with:
21
+ node-version: 18.16.1
22
+ registry-url: https://registry.npmjs.org/
23
+ - run: npm ci
24
+ - run: npm run build
25
+ - name: Git configuration
26
+ run: |
27
+ git config --local user.email "github-actions[bot]@users.noreply.github.com"
28
+ git config --local user.name "github-actions[bot]"
29
+ - run: npm version --commit-hooks true patch
30
+ - name: Git commit and push
31
+ env:
32
+ GITHUB_TOKEN: ${{ secrets.PAT }}
33
+ run: git push origin
34
+ - run: npm publish
35
+ env:
36
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
@@ -0,0 +1,2 @@
1
+ *.md
2
+ dist
package/.prettierrc ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "printWidth": 80,
3
+ "tabWidth": 2,
4
+ "singleQuote": false,
5
+ "trailingComma": "es5",
6
+ "semi": true,
7
+ "useTabs": false
8
+ }
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # mynth-logger
2
+
3
+ This repository stores configuration options for handling logs in Mynth
4
+ microservices. The `setupLogging` function is publicly exposed and can
5
+ be called in entrypoint scripts. This function overrides `console.log`
6
+ statements.
7
+
8
+ For local development, install pino-pretty via `npm install --save-dev
9
+ pino-pretty`. After calling `setupLogging()`, `console.log` calls will
10
+ output colorful statements to the terminal.
11
+
12
+ For production builds, logs are output to stdout in JSON format that can
13
+ be parsed and processed by Datadog.
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "mynth-logger",
3
+ "version": "1.0.2",
4
+ "description": "Package to format logs for mynth microservices.",
5
+ "main": "./src/index.ts",
6
+ "scripts": {
7
+ "test": "npx ava",
8
+ "build": "tsc",
9
+ "prettier": "npx prettier -w '**/*.{js,jsx,ts,tsx,json,yml.j2,yml,yaml,.*}'",
10
+ "lint": "concurrently \"npx prettier --check '**/*.{js,jsx,ts,tsx,json,yml.j2,yml,yaml,.*}'\" \"npx eslint --max-warnings=0\""
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git@github.com-mynth:MynthAI/mynth-logger.git"
15
+ },
16
+ "dependencies": {
17
+ "ava": "^5.3.1",
18
+ "json5": "^2.2.3",
19
+ "pino": "^8.15.0"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^20.5.0",
23
+ "concurrently": "^8.2.0",
24
+ "eslint": "^8.47.0",
25
+ "eslint-plugin-ava": "^14.0.0",
26
+ "pino-pretty": "^10.2.0",
27
+ "prettier": "^3.0.2",
28
+ "tsx": "^3.12.7",
29
+ "typescript": "^5.1.6"
30
+ },
31
+ "ava": {
32
+ "files": [
33
+ "tests/**/*.test.ts"
34
+ ],
35
+ "extensions": {
36
+ "ts": "module"
37
+ },
38
+ "nodeArguments": [
39
+ "--loader=tsx"
40
+ ]
41
+ }
42
+ }
package/src/index.ts ADDED
@@ -0,0 +1,74 @@
1
+ import JSON5 from "json5";
2
+ import pino, { LoggerOptions } from "pino";
3
+
4
+ const isPinoPrettyInstalled = () => {
5
+ try {
6
+ pino({ transport: { target: "pino-pretty" } });
7
+ return true;
8
+ } catch {
9
+ return false;
10
+ }
11
+ };
12
+
13
+ const pinoSettings: LoggerOptions = {
14
+ base: undefined,
15
+ timestamp: false,
16
+ level: "debug",
17
+ formatters: {
18
+ level(level) {
19
+ return { level };
20
+ },
21
+ },
22
+ };
23
+
24
+ if (isPinoPrettyInstalled() && process.env.NODE_ENV != "test") {
25
+ pinoSettings["transport"] = {
26
+ target: "pino-pretty",
27
+ options: {
28
+ colorize: true,
29
+ },
30
+ };
31
+ }
32
+
33
+ const logger = pino(pinoSettings);
34
+
35
+ const formatItem = (item: any): string => {
36
+ // Remove colors from strings
37
+ if (typeof item === "string") {
38
+ return item.replace(/\x1b\[[0-9;]*m/g, "");
39
+ }
40
+
41
+ return JSON5.stringify(item).replace(/^\'|\'$/g, "");
42
+ };
43
+
44
+ const format = (items: any[]): string => {
45
+ return Array.from(items)
46
+ .map((item) => formatItem(item))
47
+ .join(" ");
48
+ };
49
+
50
+ const setupLogging = () => {
51
+ if (typeof window !== "undefined") return;
52
+
53
+ console.log = function () {
54
+ logger.info(format(Array.from(arguments)));
55
+ };
56
+
57
+ console.info = function () {
58
+ logger.info(format(Array.from(arguments)));
59
+ };
60
+
61
+ console.warn = function () {
62
+ logger.warn(format(Array.from(arguments)));
63
+ };
64
+
65
+ console.error = function () {
66
+ logger.error(format(Array.from(arguments)));
67
+ };
68
+
69
+ console.debug = function () {
70
+ logger.debug(format(Array.from(arguments)));
71
+ };
72
+ };
73
+
74
+ export { setupLogging };
@@ -0,0 +1,8 @@
1
+ import test from "ava";
2
+ import { setupLogging } from "../src/index";
3
+
4
+ test("setupLogging can be called", (t) => {
5
+ setupLogging();
6
+ console.debug("Hello ava");
7
+ t.pass();
8
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "baseUrl": ".",
4
+ "target": "es5",
5
+ "lib": ["dom", "dom.iterable", "esnext"],
6
+ "allowJs": true,
7
+ "strict": false,
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true,
10
+ "outDir": "./dist",
11
+ "rootDir": "./src",
12
+ "jsx": "react",
13
+ "paths": {
14
+ "src": ["./src"]
15
+ }
16
+ },
17
+ "include": ["src/**/*"],
18
+ "exclude": ["node_modules", "dist", "build"],
19
+ "ts-node": {
20
+ "transpileOnly": true,
21
+ "swc": true
22
+ }
23
+ }