0
# Logging
1
2
Formatted logging system with consistent branding and error handling for CLI applications. Provides standardized output formatting with color-coded messages and NX branding.
3
4
## Capabilities
5
6
### Logger Object
7
8
Main logging utility with methods for different message types and consistent formatting.
9
10
```typescript { .api }
11
/**
12
* Main logging utility object with formatted output methods
13
*/
14
export const logger: {
15
/** Logs warning messages in yellow */
16
warn(s: string | any): void;
17
/** Logs error messages in red, handles NX-prefixed messages and Error objects with stack traces */
18
error(s: string | Error | any): void;
19
/** Logs info messages with NX branding */
20
info(s: string | any): void;
21
/** Basic console.log wrapper */
22
log(...s: any[]): void;
23
/** Console debug wrapper */
24
debug(...s: any[]): void;
25
/** Console error wrapper for fatal errors */
26
fatal(...s: any[]): void;
27
};
28
```
29
30
**Usage Examples:**
31
32
```typescript
33
import { logger } from "@nrwl/tao/shared/logger";
34
35
// Basic logging
36
logger.info("Starting workspace operation");
37
logger.warn("Configuration file not found, using defaults");
38
logger.error("Failed to read project configuration");
39
40
// Error object handling
41
try {
42
// some operation
43
} catch (error) {
44
logger.error(error); // Automatically handles stack traces
45
}
46
47
// Debug information
48
logger.debug("Processing file:", filePath);
49
50
// Fatal errors
51
logger.fatal("Critical error: workspace corrupted");
52
```
53
54
### Formatting Constants
55
56
Pre-formatted branding constants for consistent message styling.
57
58
```typescript { .api }
59
/** Formatted prefix string with cyan "NX" branding */
60
export const NX_PREFIX: string;
61
62
/** Formatted error prefix string with red "ERROR" branding */
63
export const NX_ERROR: string;
64
```
65
66
**Usage Examples:**
67
68
```typescript
69
import { NX_PREFIX, NX_ERROR } from "@nrwl/tao/shared/logger";
70
71
console.log(`${NX_PREFIX} Custom operation completed`);
72
console.log(`${NX_ERROR} Custom error occurred`);
73
```
74
75
### String Utilities
76
77
Utility functions for string formatting and processing.
78
79
```typescript { .api }
80
/**
81
* Removes common leading whitespace from multi-line strings
82
* @param str - Multi-line string with potential indentation
83
* @returns String with common leading whitespace removed
84
*/
85
export function stripIndent(str: string): string;
86
```
87
88
**Usage Examples:**
89
90
```typescript
91
import { stripIndent } from "@nrwl/tao/shared/logger";
92
93
const template = stripIndent`
94
This is a multi-line string
95
with various indentation levels
96
that will be normalized
97
`;
98
99
console.log(template);
100
// Output:
101
// This is a multi-line string
102
// with various indentation levels
103
// that will be normalized
104
```
105
106
## Integration Patterns
107
108
The logging utilities integrate seamlessly with Nx operations:
109
110
```typescript
111
import { logger } from "@nrwl/tao/shared/logger";
112
import { Workspaces } from "@nrwl/tao/shared/workspace";
113
114
function processWorkspace(root: string) {
115
logger.info("Analyzing workspace structure");
116
117
try {
118
const workspaces = new Workspaces(root);
119
const projects = workspaces.readProjectsConfigurations();
120
121
logger.info(`Found ${Object.keys(projects.projects).length} projects`);
122
} catch (error) {
123
logger.error("Failed to read workspace configuration");
124
logger.error(error);
125
}
126
}
127
```