• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dev-ignis / cross-log / 16982792483

15 Aug 2025 04:15AM UTC coverage: 85.706% (-9.9%) from 95.62%
16982792483

push

github

dev-ignis
test: - ci;

489 of 647 branches covered (75.58%)

Branch coverage included in aggregate %.

944 of 1025 relevant lines covered (92.1%)

357.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.59
/src/core/utils.ts
1
/**
2
 * Utility functions for the universal logger
3
 */
4

5
import { LogLevel, Environment } from './types';
42✔
6
import { detectEnvironment as detectEnv, getEnvironmentVariable } from './environment';
42✔
7

8

9
/**
10
 * Detect the current environment
11
 * Re-exported from environment module for backward compatibility
12
 */
13
export function detectEnvironment(): Environment {
42✔
14
  return detectEnv();
762✔
15
}
16

17
/**
18
 * Parse log level from string
19
 */
20
export function parseLogLevel(level?: string): LogLevel | null {
42✔
21
  if (!level) return null;
48✔
22
  
23
  const upperLevel = level.toUpperCase();
42✔
24
  switch (upperLevel) {
42✔
25
    case 'DEBUG': return LogLevel.DEBUG;
3✔
26
    case 'INFO': return LogLevel.INFO;
3✔
27
    case 'WARN': return LogLevel.WARN;
6✔
28
    case 'ERROR': return LogLevel.ERROR;
18✔
29
    case 'SILENT': return LogLevel.SILENT;
3✔
30
    default: return null;
9✔
31
  }
32
}
33

34
/**
35
 * Parse boolean from environment variable
36
 */
37
export function parseEnvBoolean(value?: string, defaultValue: boolean = false): boolean {
42✔
38
  if (value === undefined) return defaultValue;
75✔
39
  return value.toLowerCase() === 'true';
66✔
40
}
41

42
/**
43
 * Parse integer from environment variable
44
 */
45
export function parseEnvInt(value: string | undefined, defaultValue: number): number {
42✔
46
  if (value === undefined) return defaultValue;
18✔
47
  const parsed = parseInt(value, 10);
15✔
48
  return isNaN(parsed) ? defaultValue : parsed;
15✔
49
}
50

51
/**
52
 * Get environment variable with fallback
53
 * Now uses the environment abstraction for cross-platform compatibility
54
 */
55
export function getEnvVar(key: string, defaultValue?: string): string | undefined {
42✔
56
  return getEnvironmentVariable(key, defaultValue);
12,816✔
57
}
58

59
/**
60
 * Format timestamp for logging
61
 */
62
export function formatTimestamp(date: Date = new Date()): string {
42✔
63
  return date.toISOString();
561✔
64
}
65

66
/**
67
 * Check if logging should be enabled for the specified level
68
 */
69
export function isLoggingEnabled(
42✔
70
  currentLevel: LogLevel, 
71
  messageLevel: LogLevel, 
72
  globalEnabled: boolean
73
): boolean {
74
  return globalEnabled && messageLevel >= currentLevel && currentLevel < LogLevel.SILENT && messageLevel < LogLevel.SILENT;
1,365✔
75
}
76

77
/**
78
 * Format message with category and timestamp
79
 */
80
export function formatMessage(
42✔
81
  message: string, 
82
  category?: string, 
83
  showTimestamp: boolean = true
×
84
): string {
85
  const parts: string[] = [];
1,245✔
86
  
87
  if (showTimestamp) {
1,245✔
88
    parts.push(`[${formatTimestamp()}]`);
555✔
89
  }
90
  
91
  if (category) {
1,245✔
92
    parts.push(`[${category}]`);
507✔
93
  }
94
  
95
  parts.push(message);
1,245✔
96
  
97
  return parts.join(' ');
1,245✔
98
}
99

100
/**
101
 * Get console method for log level
102
 */
103
export function getConsoleMethod(level: LogLevel): (message?: unknown, ...optionalParams: unknown[]) => void {
42✔
104
  switch (level) {
57!
105
    case LogLevel.DEBUG:
106
      return console.debug;
15✔
107
    case LogLevel.INFO:
108
      return console.info;
12✔
109
    case LogLevel.WARN:
110
      return console.warn;
9✔
111
    case LogLevel.ERROR:
112
      return console.error;
21✔
113
    default:
114
      return console.log;
×
115
  }
116
}
117

118
/**
119
 * Create ANSI color code for terminal output
120
 */
121
export function createAnsiColor(colorCode: number): string {
42✔
122
  return `\x1b[${colorCode}m`;
531✔
123
}
124

125
/**
126
 * Reset ANSI color
127
 */
128
export function resetAnsiColor(): string {
42✔
129
  return '\x1b[0m';
528✔
130
}
131

132
/**
133
 * Safely stringify an object for logging
134
 */
135
export function safeStringify(obj: unknown): string {
42✔
136
  try {
15✔
137
    if (typeof obj === 'string') return obj;
15✔
138
    if (obj instanceof Error) return obj.message;
12✔
139
    return JSON.stringify(obj, null, 2);
9✔
140
  } catch {
141
    return '[Circular or non-serializable object]';
3✔
142
  }
143
}
144

145
/**
146
 * Debounce function to prevent log spam
147
 */
148
export function debounce<T extends (...args: unknown[]) => void>(
42✔
149
  func: T,
150
  wait: number
151
): (...args: Parameters<T>) => void {
152
  // Use a more generic timeout type that works in both browser and Node environments
153
  let timeout: ReturnType<typeof setTimeout> | undefined;
154
  
155
  return (...args: Parameters<T>) => {
6✔
156
    const later = () => {
15✔
157
      timeout = undefined;
6✔
158
      func(...args);
6✔
159
    };
160
    
161
    if (timeout !== undefined) {
15✔
162
      clearTimeout(timeout);
9✔
163
    }
164
    timeout = setTimeout(later, wait);
15✔
165
  };
166
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc