@townco/ui 0.1.14 → 0.1.15

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.
@@ -5,5 +5,6 @@
5
5
  * Contains business logic, state management, and hooks
6
6
  */
7
7
  export * from "./hooks/index.js";
8
+ export * from "./lib/logger.js";
8
9
  export * from "./schemas/index.js";
9
10
  export * from "./store/chat-store.js";
@@ -5,5 +5,6 @@
5
5
  * Contains business logic, state management, and hooks
6
6
  */
7
7
  export * from "./hooks/index.js";
8
+ export * from "./lib/logger.js";
8
9
  export * from "./schemas/index.js";
9
10
  export * from "./store/chat-store.js";
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Browser-compatible logger
3
+ * Outputs structured JSON logs to console with color-coding
4
+ */
5
+ export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal";
6
+ export declare class Logger {
7
+ private service;
8
+ private minLevel;
9
+ constructor(service: string, minLevel?: LogLevel);
10
+ private shouldLog;
11
+ private log;
12
+ trace(message: string, metadata?: Record<string, unknown>): void;
13
+ debug(message: string, metadata?: Record<string, unknown>): void;
14
+ info(message: string, metadata?: Record<string, unknown>): void;
15
+ warn(message: string, metadata?: Record<string, unknown>): void;
16
+ error(message: string, metadata?: Record<string, unknown>): void;
17
+ fatal(message: string, metadata?: Record<string, unknown>): void;
18
+ }
19
+ /**
20
+ * Create a logger instance for a service
21
+ * @param service - Service name (e.g., "gui", "http-agent", "tui")
22
+ * @param minLevel - Minimum log level to display (default: "debug")
23
+ */
24
+ export declare function createLogger(service: string, minLevel?: LogLevel): Logger;
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Browser-compatible logger
3
+ * Outputs structured JSON logs to console with color-coding
4
+ */
5
+ const LOG_LEVELS = {
6
+ trace: 0,
7
+ debug: 1,
8
+ info: 2,
9
+ warn: 3,
10
+ error: 4,
11
+ fatal: 5,
12
+ };
13
+ const _LOG_COLORS = {
14
+ trace: "#6B7280", // gray
15
+ debug: "#3B82F6", // blue
16
+ info: "#10B981", // green
17
+ warn: "#F59E0B", // orange
18
+ error: "#EF4444", // red
19
+ fatal: "#DC2626", // dark red
20
+ };
21
+ const LOG_STYLES = {
22
+ trace: "color: #6B7280",
23
+ debug: "color: #3B82F6; font-weight: bold",
24
+ info: "color: #10B981; font-weight: bold",
25
+ warn: "color: #F59E0B; font-weight: bold",
26
+ error: "color: #EF4444; font-weight: bold",
27
+ fatal: "color: #DC2626; font-weight: bold; background: #FEE2E2",
28
+ };
29
+ export class Logger {
30
+ service;
31
+ minLevel;
32
+ constructor(service, minLevel = "debug") {
33
+ this.service = service;
34
+ this.minLevel = minLevel;
35
+ // In production, suppress trace and debug logs
36
+ if (typeof process !== "undefined" &&
37
+ process.env?.NODE_ENV === "production") {
38
+ this.minLevel = "info";
39
+ }
40
+ }
41
+ shouldLog(level) {
42
+ return LOG_LEVELS[level] >= LOG_LEVELS[this.minLevel];
43
+ }
44
+ log(level, message, metadata) {
45
+ if (!this.shouldLog(level)) {
46
+ return;
47
+ }
48
+ const entry = {
49
+ timestamp: new Date().toISOString(),
50
+ level,
51
+ service: this.service,
52
+ message,
53
+ ...(metadata && { metadata }),
54
+ };
55
+ // Console output with color-coding
56
+ const style = LOG_STYLES[level];
57
+ const levelUpper = level.toUpperCase().padEnd(5);
58
+ if (typeof console !== "undefined") {
59
+ // Format: [timestamp] [SERVICE] [LEVEL] message
60
+ const prefix = `%c[${entry.timestamp}] [${this.service}] [${levelUpper}]`;
61
+ const msg = metadata ? `${message} %o` : message;
62
+ switch (level) {
63
+ case "trace":
64
+ case "debug":
65
+ case "info":
66
+ console.log(prefix, style, msg, ...(metadata ? [metadata] : []));
67
+ break;
68
+ case "warn":
69
+ console.warn(prefix, style, msg, ...(metadata ? [metadata] : []));
70
+ break;
71
+ case "error":
72
+ case "fatal":
73
+ console.error(prefix, style, msg, ...(metadata ? [metadata] : []));
74
+ break;
75
+ }
76
+ }
77
+ // Also log the structured JSON for debugging
78
+ if (level === "fatal" || level === "error") {
79
+ console.debug("Structured log:", JSON.stringify(entry));
80
+ }
81
+ }
82
+ trace(message, metadata) {
83
+ this.log("trace", message, metadata);
84
+ }
85
+ debug(message, metadata) {
86
+ this.log("debug", message, metadata);
87
+ }
88
+ info(message, metadata) {
89
+ this.log("info", message, metadata);
90
+ }
91
+ warn(message, metadata) {
92
+ this.log("warn", message, metadata);
93
+ }
94
+ error(message, metadata) {
95
+ this.log("error", message, metadata);
96
+ }
97
+ fatal(message, metadata) {
98
+ this.log("fatal", message, metadata);
99
+ }
100
+ }
101
+ /**
102
+ * Create a logger instance for a service
103
+ * @param service - Service name (e.g., "gui", "http-agent", "tui")
104
+ * @param minLevel - Minimum log level to display (default: "debug")
105
+ */
106
+ export function createLogger(service, minLevel = "debug") {
107
+ return new Logger(service, minLevel);
108
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@townco/ui",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -59,7 +59,7 @@
59
59
  },
60
60
  "devDependencies": {
61
61
  "@tailwindcss/postcss": "^4.1.17",
62
- "@townco/tsconfig": "0.1.11",
62
+ "@townco/tsconfig": "0.1.12",
63
63
  "@types/node": "^24.10.0",
64
64
  "@types/react": "^19.2.2",
65
65
  "ink": "^6.4.0",