@pgpmjs/logger 1.3.0
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.
- package/LICENSE +23 -0
- package/README.md +143 -0
- package/esm/index.js +1 -0
- package/esm/logger.js +107 -0
- package/index.d.ts +1 -0
- package/index.js +5 -0
- package/logger.d.ts +15 -0
- package/logger.js +118 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2025 Constructive <developers@constructive.io>
|
|
5
|
+
Copyright (c) 2020-present, Interweb, Inc.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# @launchql/logger
|
|
2
|
+
|
|
3
|
+
<p align="center" width="100%">
|
|
4
|
+
<img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
Simple logger utility with yanse colors for consistent logging across packages.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @launchql/logger
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- Colored console output using yanse
|
|
18
|
+
- Consistent log formatting
|
|
19
|
+
- Namespace support for identifying log sources
|
|
20
|
+
- Simple API with log, error, and warn methods
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { Logger } from '@launchql/logger';
|
|
26
|
+
|
|
27
|
+
// Create a logger with a namespace
|
|
28
|
+
const log = new Logger('my-module');
|
|
29
|
+
|
|
30
|
+
// Log messages
|
|
31
|
+
log.log('This is a regular message');
|
|
32
|
+
log.error('This is an error message');
|
|
33
|
+
log.warn('This is a warning message');
|
|
34
|
+
|
|
35
|
+
// Messages will be prefixed with the namespace in color
|
|
36
|
+
// [my-module] This is a regular message
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## API
|
|
40
|
+
|
|
41
|
+
### `new Logger(namespace: string)`
|
|
42
|
+
|
|
43
|
+
Creates a new logger instance with the given namespace.
|
|
44
|
+
|
|
45
|
+
### Methods
|
|
46
|
+
|
|
47
|
+
- `log(...args: any[]): void` - Log a regular message (cyan namespace)
|
|
48
|
+
- `error(...args: any[]): void` - Log an error message (red namespace)
|
|
49
|
+
- `warn(...args: any[]): void` - Log a warning message (yellow namespace)
|
|
50
|
+
|
|
51
|
+
## Example
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { Logger } from '@launchql/logger';
|
|
55
|
+
|
|
56
|
+
const dbLog = new Logger('database');
|
|
57
|
+
const apiLog = new Logger('api');
|
|
58
|
+
|
|
59
|
+
dbLog.log('Connected to PostgreSQL');
|
|
60
|
+
// Output: [database] Connected to PostgreSQL (with cyan [database])
|
|
61
|
+
|
|
62
|
+
apiLog.error('Failed to fetch user:', error);
|
|
63
|
+
// Output: [api] Failed to fetch user: ... (with red [api])
|
|
64
|
+
|
|
65
|
+
apiLog.warn('Rate limit approaching');
|
|
66
|
+
// Output: [api] Rate limit approaching (with yellow [api])
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## Education and Tutorials
|
|
72
|
+
|
|
73
|
+
1. 🚀 [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
|
|
74
|
+
Get started with modular databases in minutes. Install prerequisites and deploy your first module.
|
|
75
|
+
|
|
76
|
+
2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
|
|
77
|
+
Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
|
|
78
|
+
|
|
79
|
+
3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
|
|
80
|
+
Master the workflow for adding, organizing, and managing database changes with pgpm.
|
|
81
|
+
|
|
82
|
+
4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
|
|
83
|
+
Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
|
|
84
|
+
|
|
85
|
+
5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
|
|
86
|
+
Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
|
|
87
|
+
|
|
88
|
+
6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
|
|
89
|
+
Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
|
|
90
|
+
|
|
91
|
+
7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
|
|
92
|
+
Common issues and solutions for pgpm, PostgreSQL, and testing.
|
|
93
|
+
|
|
94
|
+
## Related Constructive Tooling
|
|
95
|
+
|
|
96
|
+
### 🧪 Testing
|
|
97
|
+
|
|
98
|
+
* [pgsql-test](https://github.com/constructive-io/constructive/tree/main/packages/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
|
|
99
|
+
* [supabase-test](https://github.com/constructive-io/constructive/tree/main/packages/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
|
|
100
|
+
* [graphile-test](https://github.com/constructive-io/constructive/tree/main/packages/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
|
|
101
|
+
* [pg-query-context](https://github.com/constructive-io/constructive/tree/main/packages/pg-query-context): **🔒 Session context injection** to add session-local context (e.g., `SET LOCAL`) into queries—ideal for setting `role`, `jwt.claims`, and other session settings.
|
|
102
|
+
|
|
103
|
+
### 🧠 Parsing & AST
|
|
104
|
+
|
|
105
|
+
* [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
106
|
+
* [libpg-query-node](https://www.npmjs.com/package/libpg-query): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
107
|
+
* [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
108
|
+
* [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
|
|
109
|
+
* [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
|
|
110
|
+
* [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
|
|
111
|
+
* [pg-ast](https://www.npmjs.com/package/pg-ast): **🔍 Low-level AST tools** and transformations for Postgres query structures.
|
|
112
|
+
|
|
113
|
+
### 🚀 API & Dev Tools
|
|
114
|
+
|
|
115
|
+
* [launchql/server](https://github.com/constructive-io/constructive/tree/main/packages/server): **⚡ Express-based API server** powered by PostGraphile to expose a secure, scalable GraphQL API over your Postgres database.
|
|
116
|
+
* [launchql/explorer](https://github.com/constructive-io/constructive/tree/main/packages/explorer): **🔎 Visual API explorer** with GraphiQL for browsing across all databases and schemas—useful for debugging, documentation, and API prototyping.
|
|
117
|
+
|
|
118
|
+
### 🔁 Streaming & Uploads
|
|
119
|
+
|
|
120
|
+
* [launchql/s3-streamer](https://github.com/constructive-io/constructive/tree/main/packages/s3-streamer): **📤 Direct S3 streaming** for large files with support for metadata injection and content validation.
|
|
121
|
+
* [launchql/etag-hash](https://github.com/constructive-io/constructive/tree/main/packages/etag-hash): **🏷️ S3-compatible ETags** created by streaming and hashing file uploads in chunks.
|
|
122
|
+
* [launchql/etag-stream](https://github.com/constructive-io/constructive/tree/main/packages/etag-stream): **🔄 ETag computation** via Node stream transformer during upload or transfer.
|
|
123
|
+
* [launchql/uuid-hash](https://github.com/constructive-io/constructive/tree/main/packages/uuid-hash): **🆔 Deterministic UUIDs** generated from hashed content, great for deduplication and asset referencing.
|
|
124
|
+
* [launchql/uuid-stream](https://github.com/constructive-io/constructive/tree/main/packages/uuid-stream): **🌊 Streaming UUID generation** based on piped file content—ideal for upload pipelines.
|
|
125
|
+
* [launchql/upload-names](https://github.com/constructive-io/constructive/tree/main/packages/upload-names): **📂 Collision-resistant filenames** utility for structured and unique file names for uploads.
|
|
126
|
+
|
|
127
|
+
### 🧰 CLI & Codegen
|
|
128
|
+
|
|
129
|
+
* [pgpm](https://github.com/constructive-io/constructive/tree/main/packages/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
|
|
130
|
+
* [@launchql/cli](https://github.com/constructive-io/constructive/tree/main/packages/cli): **🖥️ Command-line toolkit** for managing LaunchQL projects—supports database scaffolding, migrations, seeding, code generation, and automation.
|
|
131
|
+
* [constructive-io/constructive-gen](https://github.com/constructive-io/constructive/tree/main/packages/launchql-gen): **✨ Auto-generated GraphQL** mutations and queries dynamically built from introspected schema data.
|
|
132
|
+
* [@launchql/query-builder](https://github.com/constructive-io/constructive/tree/main/packages/query-builder): **🏗️ SQL constructor** providing a robust TypeScript-based query builder for dynamic generation of `SELECT`, `INSERT`, `UPDATE`, `DELETE`, and stored procedure calls—supports advanced SQL features like `JOIN`, `GROUP BY`, and schema-qualified queries.
|
|
133
|
+
* [@launchql/query](https://github.com/constructive-io/constructive/tree/main/packages/query): **🧩 Fluent GraphQL builder** for PostGraphile schemas. ⚡ Schema-aware via introspection, 🧩 composable and ergonomic for building deeply nested queries.
|
|
134
|
+
|
|
135
|
+
## Credits
|
|
136
|
+
|
|
137
|
+
**🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
|
|
138
|
+
|
|
139
|
+
## Disclaimer
|
|
140
|
+
|
|
141
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
142
|
+
|
|
143
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
package/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Logger } from './logger';
|
package/esm/logger.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import yanse from 'yanse';
|
|
2
|
+
const levelPriority = {
|
|
3
|
+
debug: 0,
|
|
4
|
+
info: 1,
|
|
5
|
+
success: 2,
|
|
6
|
+
warn: 3,
|
|
7
|
+
error: 4
|
|
8
|
+
};
|
|
9
|
+
const levelColors = {
|
|
10
|
+
info: yanse.cyan,
|
|
11
|
+
warn: yanse.yellow,
|
|
12
|
+
error: yanse.red,
|
|
13
|
+
debug: yanse.gray,
|
|
14
|
+
success: yanse.green
|
|
15
|
+
};
|
|
16
|
+
const hasAnsi = (text) => {
|
|
17
|
+
return typeof text === 'string' && /\u001b\[\d+m/.test(text);
|
|
18
|
+
};
|
|
19
|
+
// Parse LOG_LEVEL from environment
|
|
20
|
+
let globalLogLevel = process.env.LOG_LEVEL?.toLowerCase() ?? 'info';
|
|
21
|
+
// Update log level at runtime
|
|
22
|
+
export const setLogLevel = (level) => {
|
|
23
|
+
if (levelPriority[level] !== undefined) {
|
|
24
|
+
globalLogLevel = level;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
// Parse LOG_TIMESTAMP from environment (default: false)
|
|
28
|
+
let showTimestamp = process.env.LOG_TIMESTAMP?.toLowerCase() === 'true' ||
|
|
29
|
+
process.env.LOG_TIMESTAMP === '1';
|
|
30
|
+
// Update timestamp display at runtime
|
|
31
|
+
export const setShowTimestamp = (show) => {
|
|
32
|
+
showTimestamp = show;
|
|
33
|
+
};
|
|
34
|
+
const parseScopeFilter = (env) => {
|
|
35
|
+
const include = new Set();
|
|
36
|
+
const exclude = new Set();
|
|
37
|
+
(env ?? '').split(',').map(s => s.trim()).forEach(scope => {
|
|
38
|
+
if (!scope)
|
|
39
|
+
return;
|
|
40
|
+
if (scope === '*') {
|
|
41
|
+
include.add('*');
|
|
42
|
+
}
|
|
43
|
+
else if (scope.startsWith('^')) {
|
|
44
|
+
exclude.add(scope.slice(1));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
include.add(scope);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
return { include, exclude };
|
|
51
|
+
};
|
|
52
|
+
let { include: allowedScopes, exclude: blockedScopes } = parseScopeFilter(process.env.LOG_SCOPE);
|
|
53
|
+
// Update scopes at runtime
|
|
54
|
+
export const setLogScopes = (scopes) => {
|
|
55
|
+
const parsed = parseScopeFilter(scopes.join(','));
|
|
56
|
+
allowedScopes = parsed.include;
|
|
57
|
+
blockedScopes = parsed.exclude;
|
|
58
|
+
};
|
|
59
|
+
// Logger class
|
|
60
|
+
export class Logger {
|
|
61
|
+
scope;
|
|
62
|
+
constructor(scope) {
|
|
63
|
+
this.scope = scope;
|
|
64
|
+
}
|
|
65
|
+
log(level, ...args) {
|
|
66
|
+
// Respect log level
|
|
67
|
+
if (levelPriority[level] < levelPriority[globalLogLevel])
|
|
68
|
+
return;
|
|
69
|
+
// Scope filtering
|
|
70
|
+
if (blockedScopes.has(this.scope))
|
|
71
|
+
return;
|
|
72
|
+
if (allowedScopes.size > 0 &&
|
|
73
|
+
!allowedScopes.has('*') &&
|
|
74
|
+
!allowedScopes.has(this.scope)) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const tag = yanse.bold(`[${this.scope}]`);
|
|
78
|
+
const color = levelColors[level];
|
|
79
|
+
const prefix = color(`${level.toUpperCase()}:`);
|
|
80
|
+
const formattedArgs = args.map(arg => typeof arg === 'string' && !hasAnsi(arg) ? color(arg) : arg);
|
|
81
|
+
const stream = level === 'error' ? process.stderr : process.stdout;
|
|
82
|
+
const outputParts = showTimestamp
|
|
83
|
+
? [yanse.dim(`[${new Date().toISOString()}]`), tag, prefix, ...formattedArgs]
|
|
84
|
+
: [tag, prefix, ...formattedArgs];
|
|
85
|
+
const output = outputParts
|
|
86
|
+
.map(arg => String(arg))
|
|
87
|
+
.join(' ') + '\n';
|
|
88
|
+
stream.write(output);
|
|
89
|
+
}
|
|
90
|
+
info(...args) {
|
|
91
|
+
this.log('info', ...args);
|
|
92
|
+
}
|
|
93
|
+
warn(...args) {
|
|
94
|
+
this.log('warn', ...args);
|
|
95
|
+
}
|
|
96
|
+
error(...args) {
|
|
97
|
+
this.log('error', ...args);
|
|
98
|
+
}
|
|
99
|
+
debug(...args) {
|
|
100
|
+
this.log('debug', ...args);
|
|
101
|
+
}
|
|
102
|
+
success(...args) {
|
|
103
|
+
this.log('success', ...args);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Factory function
|
|
107
|
+
export const createLogger = (scope) => new Logger(scope);
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Logger } from './logger';
|
package/index.js
ADDED
package/logger.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type LogLevel = 'info' | 'warn' | 'error' | 'debug' | 'success';
|
|
2
|
+
export declare const setLogLevel: (level: LogLevel) => void;
|
|
3
|
+
export declare const setShowTimestamp: (show: boolean) => void;
|
|
4
|
+
export declare const setLogScopes: (scopes: string[]) => void;
|
|
5
|
+
export declare class Logger {
|
|
6
|
+
private scope;
|
|
7
|
+
constructor(scope: string);
|
|
8
|
+
private log;
|
|
9
|
+
info(...args: any[]): void;
|
|
10
|
+
warn(...args: any[]): void;
|
|
11
|
+
error(...args: any[]): void;
|
|
12
|
+
debug(...args: any[]): void;
|
|
13
|
+
success(...args: any[]): void;
|
|
14
|
+
}
|
|
15
|
+
export declare const createLogger: (scope: string) => Logger;
|
package/logger.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createLogger = exports.Logger = exports.setLogScopes = exports.setShowTimestamp = exports.setLogLevel = void 0;
|
|
7
|
+
const yanse_1 = __importDefault(require("yanse"));
|
|
8
|
+
const levelPriority = {
|
|
9
|
+
debug: 0,
|
|
10
|
+
info: 1,
|
|
11
|
+
success: 2,
|
|
12
|
+
warn: 3,
|
|
13
|
+
error: 4
|
|
14
|
+
};
|
|
15
|
+
const levelColors = {
|
|
16
|
+
info: yanse_1.default.cyan,
|
|
17
|
+
warn: yanse_1.default.yellow,
|
|
18
|
+
error: yanse_1.default.red,
|
|
19
|
+
debug: yanse_1.default.gray,
|
|
20
|
+
success: yanse_1.default.green
|
|
21
|
+
};
|
|
22
|
+
const hasAnsi = (text) => {
|
|
23
|
+
return typeof text === 'string' && /\u001b\[\d+m/.test(text);
|
|
24
|
+
};
|
|
25
|
+
// Parse LOG_LEVEL from environment
|
|
26
|
+
let globalLogLevel = process.env.LOG_LEVEL?.toLowerCase() ?? 'info';
|
|
27
|
+
// Update log level at runtime
|
|
28
|
+
const setLogLevel = (level) => {
|
|
29
|
+
if (levelPriority[level] !== undefined) {
|
|
30
|
+
globalLogLevel = level;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
exports.setLogLevel = setLogLevel;
|
|
34
|
+
// Parse LOG_TIMESTAMP from environment (default: false)
|
|
35
|
+
let showTimestamp = process.env.LOG_TIMESTAMP?.toLowerCase() === 'true' ||
|
|
36
|
+
process.env.LOG_TIMESTAMP === '1';
|
|
37
|
+
// Update timestamp display at runtime
|
|
38
|
+
const setShowTimestamp = (show) => {
|
|
39
|
+
showTimestamp = show;
|
|
40
|
+
};
|
|
41
|
+
exports.setShowTimestamp = setShowTimestamp;
|
|
42
|
+
const parseScopeFilter = (env) => {
|
|
43
|
+
const include = new Set();
|
|
44
|
+
const exclude = new Set();
|
|
45
|
+
(env ?? '').split(',').map(s => s.trim()).forEach(scope => {
|
|
46
|
+
if (!scope)
|
|
47
|
+
return;
|
|
48
|
+
if (scope === '*') {
|
|
49
|
+
include.add('*');
|
|
50
|
+
}
|
|
51
|
+
else if (scope.startsWith('^')) {
|
|
52
|
+
exclude.add(scope.slice(1));
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
include.add(scope);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return { include, exclude };
|
|
59
|
+
};
|
|
60
|
+
let { include: allowedScopes, exclude: blockedScopes } = parseScopeFilter(process.env.LOG_SCOPE);
|
|
61
|
+
// Update scopes at runtime
|
|
62
|
+
const setLogScopes = (scopes) => {
|
|
63
|
+
const parsed = parseScopeFilter(scopes.join(','));
|
|
64
|
+
allowedScopes = parsed.include;
|
|
65
|
+
blockedScopes = parsed.exclude;
|
|
66
|
+
};
|
|
67
|
+
exports.setLogScopes = setLogScopes;
|
|
68
|
+
// Logger class
|
|
69
|
+
class Logger {
|
|
70
|
+
scope;
|
|
71
|
+
constructor(scope) {
|
|
72
|
+
this.scope = scope;
|
|
73
|
+
}
|
|
74
|
+
log(level, ...args) {
|
|
75
|
+
// Respect log level
|
|
76
|
+
if (levelPriority[level] < levelPriority[globalLogLevel])
|
|
77
|
+
return;
|
|
78
|
+
// Scope filtering
|
|
79
|
+
if (blockedScopes.has(this.scope))
|
|
80
|
+
return;
|
|
81
|
+
if (allowedScopes.size > 0 &&
|
|
82
|
+
!allowedScopes.has('*') &&
|
|
83
|
+
!allowedScopes.has(this.scope)) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const tag = yanse_1.default.bold(`[${this.scope}]`);
|
|
87
|
+
const color = levelColors[level];
|
|
88
|
+
const prefix = color(`${level.toUpperCase()}:`);
|
|
89
|
+
const formattedArgs = args.map(arg => typeof arg === 'string' && !hasAnsi(arg) ? color(arg) : arg);
|
|
90
|
+
const stream = level === 'error' ? process.stderr : process.stdout;
|
|
91
|
+
const outputParts = showTimestamp
|
|
92
|
+
? [yanse_1.default.dim(`[${new Date().toISOString()}]`), tag, prefix, ...formattedArgs]
|
|
93
|
+
: [tag, prefix, ...formattedArgs];
|
|
94
|
+
const output = outputParts
|
|
95
|
+
.map(arg => String(arg))
|
|
96
|
+
.join(' ') + '\n';
|
|
97
|
+
stream.write(output);
|
|
98
|
+
}
|
|
99
|
+
info(...args) {
|
|
100
|
+
this.log('info', ...args);
|
|
101
|
+
}
|
|
102
|
+
warn(...args) {
|
|
103
|
+
this.log('warn', ...args);
|
|
104
|
+
}
|
|
105
|
+
error(...args) {
|
|
106
|
+
this.log('error', ...args);
|
|
107
|
+
}
|
|
108
|
+
debug(...args) {
|
|
109
|
+
this.log('debug', ...args);
|
|
110
|
+
}
|
|
111
|
+
success(...args) {
|
|
112
|
+
this.log('success', ...args);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.Logger = Logger;
|
|
116
|
+
// Factory function
|
|
117
|
+
const createLogger = (scope) => new Logger(scope);
|
|
118
|
+
exports.createLogger = createLogger;
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pgpmjs/logger",
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"author": "Constructive <developers@constructive.io>",
|
|
5
|
+
"description": "Simple logger utility",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/constructive-io/constructive",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/constructive-io/constructive"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/constructive-io/constructive/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"clean": "makage clean",
|
|
24
|
+
"prepack": "npm run build",
|
|
25
|
+
"build": "makage build",
|
|
26
|
+
"build:dev": "makage build --dev",
|
|
27
|
+
"lint": "eslint . --fix",
|
|
28
|
+
"test": "jest",
|
|
29
|
+
"test:watch": "jest --watch"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"yanse": "^0.1.5"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"logger",
|
|
36
|
+
"logging",
|
|
37
|
+
"yanse",
|
|
38
|
+
"console",
|
|
39
|
+
"pgpm",
|
|
40
|
+
"pgpmjs"
|
|
41
|
+
],
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"makage": "^0.1.8"
|
|
44
|
+
},
|
|
45
|
+
"gitHead": "e4d5396a5d9154f4886176bb00c2b460b0f320e5"
|
|
46
|
+
}
|