@amit_mandal/smart-logger-devtools 1.0.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/SmartLogger DevTools.md +156 -0
- package/SmartLogger.md +113 -0
- package/amit_mandal-smart-logger-devtools-1.0.0.tgz +0 -0
- package/dist/index.d.mts +55 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.js +376 -0
- package/dist/index.mjs +348 -0
- package/package.json +46 -0
- package/src/SmartLoggerDevTools.tsx +49 -0
- package/src/core/eventEmitter.ts +30 -0
- package/src/core/logger.ts +114 -0
- package/src/global.d.ts +1 -0
- package/src/hooks/useLogHistory.ts +37 -0
- package/src/index.ts +6 -0
- package/src/styles/index.css +321 -0
- package/src/types.ts +22 -0
- package/src/ui/FloatingButton.tsx +24 -0
- package/src/ui/LogModal.tsx +97 -0
- package/src/utils/formatters.ts +29 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# SmartLogger DevTools
|
|
2
|
+
|
|
3
|
+
A reusable JavaScript/TypeScript package that intercepts console logs and provides a floating UI with a modal log viewer
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Console Interception**: Intercepts `console.log`, `warn`, `error`, `info`, and `debug` methods.
|
|
8
|
+
- **Configurable Logging**: Control log suppression, timestamp display, component name display, and active log levels.
|
|
9
|
+
- **Log History**: Stores a configurable number of recent log entries in memory.
|
|
10
|
+
- **Floating UI**: A discreet floating button to open the log viewer modal.
|
|
11
|
+
- **Interactive Log Modal**: Displays logs with filtering by level, search functionality, and the ability to clear logs.
|
|
12
|
+
- **Production Suppression**: Automatically suppresses logs in production environments by default.
|
|
13
|
+
- **Type-safe**: Written in TypeScript with comprehensive type definitions.
|
|
14
|
+
- **Framework Compatible**: Designed for seamless integration with React and Next.js projects.
|
|
15
|
+
- **Clean Up**: Provides a method to restore original console behavior.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @amit_mandal/smart-logger-devtools
|
|
21
|
+
# or
|
|
22
|
+
yarn add @amit_mandal/smart-logger-devtools
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### 1. Initialize SmartLogger
|
|
28
|
+
|
|
29
|
+
Initialize the `SmartLogger` early in your application, for example, in `_app.tsx` (Next.js) or `main.tsx` (React).
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
// src/main.tsx or pages/_app.tsx
|
|
33
|
+
import React from 'react';
|
|
34
|
+
import ReactDOM from 'react-dom/client';
|
|
35
|
+
import App from './App';
|
|
36
|
+
import { SmartLogger } from '@amit_mandal/smart-logger-devtools';
|
|
37
|
+
|
|
38
|
+
// Initialize SmartLogger before your application renders
|
|
39
|
+
SmartLogger.init({
|
|
40
|
+
enabled: true, // Set to false to suppress all logs
|
|
41
|
+
showTimestamp: true, // Prepend timestamp to logs
|
|
42
|
+
showComponentName: true, // Prepend component name if provided
|
|
43
|
+
maxHistory: 100, // Store last 100 logs
|
|
44
|
+
level: ['log', 'warn', 'error', 'info', 'debug'], // Only show these log levels
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
ReactDOM.createRoot(document.getElementById('root')!).render(
|
|
48
|
+
<React.StrictMode>
|
|
49
|
+
<App />
|
|
50
|
+
</React.StrictMode>,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
// Example logs after initialization
|
|
54
|
+
console.log('[App] Application started.');
|
|
55
|
+
console.warn('[Auth] User session expiring soon.');
|
|
56
|
+
console.error('[API] Failed to fetch data.', { status: 500 });
|
|
57
|
+
console.info('This is an info message.');
|
|
58
|
+
console.debug('This is a debug message.');
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 2. Add the DevTools Component to Your App
|
|
62
|
+
|
|
63
|
+
Place the `SmartLoggerDevTools` component at the root level of your React application. This component renders the floating button and the log modal.
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
// src/App.tsx
|
|
67
|
+
import React from 'react';
|
|
68
|
+
import { SmartLoggerDevTools } from '@amit_mandal/smart-logger-devtools';
|
|
69
|
+
|
|
70
|
+
function App() {
|
|
71
|
+
return (
|
|
72
|
+
<div>
|
|
73
|
+
<h1>My Application</h1>
|
|
74
|
+
{/* Your application content goes here */}
|
|
75
|
+
|
|
76
|
+
{/* Add the DevTools component, ideally at the root of your app */}
|
|
77
|
+
<SmartLoggerDevTools />
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export default App;
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Configuration Options
|
|
86
|
+
|
|
87
|
+
These options are passed to `SmartLogger.init(config)`:
|
|
88
|
+
|
|
89
|
+
| Option | Type | Default | Description |
|
|
90
|
+
| :---------------- | :----------- | :-------------------------------------------- | :------------------------------------------------------------------------ |
|
|
91
|
+
| `enabled` | `boolean` | `process.env.NODE_ENV !== 'production'` | If `false`, all logs are suppressed and not stored. |
|
|
92
|
+
| `showTimestamp` | `boolean` | `true` | Prepends an ISO-formatted timestamp to each log in the console. |
|
|
93
|
+
| `showComponentName` | `boolean` | `false` | Prepends a component/module name (e.g., `[ComponentName]`) to logs in the console. |
|
|
94
|
+
| `maxHistory` | `number` | `100` | The maximum number of log entries to store in memory and display in the modal. |
|
|
95
|
+
| `level` | `LogLevel[]` | `['log', 'warn', 'error', 'info', 'debug']` | An array of log levels to enable. Other levels will be suppressed from history and console output. |
|
|
96
|
+
|
|
97
|
+
### Utility Methods
|
|
98
|
+
|
|
99
|
+
#### `SmartLogger.getHistory()`
|
|
100
|
+
|
|
101
|
+
Retrieves the array of stored log entries.
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { SmartLogger, LogEntry } from '@amit_mandal/smart-logger-devtools';
|
|
105
|
+
|
|
106
|
+
const history: LogEntry[] = SmartLogger.getHistory();
|
|
107
|
+
console.log('Current log history:', history);
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### `SmartLogger.clearHistory()`
|
|
111
|
+
|
|
112
|
+
Clears all stored log entries from the history and the modal display.
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { SmartLogger } from '@amit_mandal/smart-logger-devtools';
|
|
116
|
+
|
|
117
|
+
SmartLogger.clearHistory();
|
|
118
|
+
console.log('History after clearing:', SmartLogger.getHistory());
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
#### `SmartLogger.destroy()`
|
|
122
|
+
|
|
123
|
+
Restores the original `console` methods and cleans up the logger's internal state. This is useful for testing or when you no longer need `SmartLogger`.
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import { SmartLogger } from '@amit_mandal/smart-logger-devtools';
|
|
127
|
+
|
|
128
|
+
SmartLogger.destroy();
|
|
129
|
+
console.log('SmartLogger destroyed. Original console behavior restored.');
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Log Entry Structure
|
|
133
|
+
|
|
134
|
+
Each log entry stored in the history has the following structure:
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
interface LogEntry {
|
|
138
|
+
id: string; // Unique ID for the log entry
|
|
139
|
+
timestamp: string; // ISO-formatted timestamp
|
|
140
|
+
level: 'log' | 'warn' | 'error' | 'info' | 'debug';
|
|
141
|
+
message: string; // The main log message
|
|
142
|
+
args: any[]; // Additional arguments passed to the console method
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Development vs. Production
|
|
147
|
+
|
|
148
|
+
By default, `SmartLogger` is `enabled` in development environments (`process.env.NODE_ENV !== 'production'`) and `disabled` in production. When disabled, all console logs are suppressed from history and the modal, and only the original console methods are called (if `enabled` is explicitly `false`, even original calls might be suppressed depending on the `level` configuration). You can override this behavior with the `enabled` configuration option.
|
|
149
|
+
|
|
150
|
+
## Contributing
|
|
151
|
+
|
|
152
|
+
Feel free to open issues or pull requests on the GitHub repository.
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
This project is licensed under the MIT License.
|
package/SmartLogger.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# SmartLogger
|
|
2
|
+
|
|
3
|
+
A reusable JavaScript/TypeScript package that intercepts console logs and provides configurable logging with history management.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Configurable Logging**: Control log suppression, timestamp display, component name display, and active log levels.
|
|
8
|
+
- **Log History**: Stores a configurable number of recent log entries in memory.
|
|
9
|
+
- **Production Suppression**: Automatically suppresses logs in production environments by default.
|
|
10
|
+
- **Type-safe**: Written in TypeScript with comprehensive type definitions.
|
|
11
|
+
- **Framework Compatible**: Designed to be compatible with modern JavaScript frameworks like React and Next.js.
|
|
12
|
+
- **Clean Up**: Provides a method to restore original console behavior.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @amit_mandal/smart-logger-devtools
|
|
18
|
+
# or
|
|
19
|
+
yarn add @amit_mandal/smart-logger-devtools
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Initialization
|
|
25
|
+
|
|
26
|
+
Initialize the `SmartLogger` early in your application, for example, in `app.tsx` or `main.tsx`.
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { SmartLogger } from '@amit_mandal/smart-logger-devtools';
|
|
30
|
+
|
|
31
|
+
SmartLogger.init({
|
|
32
|
+
enabled: true, // or false to suppress all logs
|
|
33
|
+
showTimestamp: true, // prepend timestamp to logs
|
|
34
|
+
showComponentName: true, // prepend component name if provided
|
|
35
|
+
maxHistory: 50, // store last 50 logs
|
|
36
|
+
level: ['log', 'warn', 'error'], // only show these log levels
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Now, your console methods are intercepted
|
|
40
|
+
console.log('[App] Application started.');
|
|
41
|
+
console.warn('[Auth] User session expiring soon.');
|
|
42
|
+
console.error('[API] Failed to fetch data.', { status: 500 });
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Configuration Options
|
|
46
|
+
|
|
47
|
+
| Option | Type | Default | Description |
|
|
48
|
+
| :---------------- | :-------- | :-------------------------------------------- | :------------------------------------------------------------------------ |
|
|
49
|
+
| `enabled` | `boolean` | `process.env.NODE_ENV !== 'production'` | If `false`, all logs are suppressed. |
|
|
50
|
+
| `showTimestamp` | `boolean` | `true` | Prepends an ISO-formatted timestamp to each log. |
|
|
51
|
+
| `showComponentName` | `boolean` | `false` | Prepends a component/module name (e.g., `[ComponentName]`) to logs. |
|
|
52
|
+
| `maxHistory` | `number` | `100` | The maximum number of log entries to store in memory. |
|
|
53
|
+
| `level` | `LogLevel[]` | `['log', 'warn', 'error', 'info', 'debug']` | An array of log levels to enable. Other levels will be suppressed. |
|
|
54
|
+
|
|
55
|
+
### Utility Methods
|
|
56
|
+
|
|
57
|
+
#### `SmartLogger.getHistory()`
|
|
58
|
+
|
|
59
|
+
Retrieves the array of stored log entries.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { SmartLogger, LogEntry } from '@amit_mandal/smart-logger-devtools';
|
|
63
|
+
|
|
64
|
+
const history: LogEntry[] = SmartLogger.getHistory();
|
|
65
|
+
console.log('Current log history:', history);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### `SmartLogger.clearHistory()`
|
|
69
|
+
|
|
70
|
+
Clears all stored log entries from the history.
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { SmartLogger } from '@amit_mandal/smart-logger-devtools';
|
|
74
|
+
|
|
75
|
+
SmartLogger.clearHistory();
|
|
76
|
+
console.log('History after clearing:', SmartLogger.getHistory());
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
#### `SmartLogger.destroy()`
|
|
80
|
+
|
|
81
|
+
Restores the original `console` methods and cleans up the logger's internal state. This is useful for testing or when you no longer need `SmartLogger`.
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { SmartLogger } from '@amit_mandal/smart-logger-devtools';
|
|
85
|
+
|
|
86
|
+
SmartLogger.destroy();
|
|
87
|
+
console.log('SmartLogger destroyed. Original console behavior restored.');
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Log Entry Structure
|
|
91
|
+
|
|
92
|
+
Each log entry stored in the history has the following structure:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
interface LogEntry {
|
|
96
|
+
timestamp: string; // ISO-formatted timestamp
|
|
97
|
+
level: 'log' | 'warn' | 'error' | 'info' | 'debug';
|
|
98
|
+
message: string; // The main log message
|
|
99
|
+
args: any[]; // Additional arguments passed to the console method
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Development vs. Production
|
|
104
|
+
|
|
105
|
+
By default, `SmartLogger` is `enabled` in development environments (`process.env.NODE_ENV !== 'production'`) and `disabled` in production. When disabled, all console logs are suppressed, and no history is stored. You can override this behavior with the `enabled` configuration option.
|
|
106
|
+
|
|
107
|
+
## Contributing
|
|
108
|
+
|
|
109
|
+
Feel free to open issues or pull requests on the GitHub repository.
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
This project is licensed under the MIT License.
|
|
Binary file
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type LogLevel = "log" | "warn" | "error" | "info" | "debug";
|
|
4
|
+
interface SmartLoggerConfig {
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
showTimestamp?: boolean;
|
|
7
|
+
showComponentName?: boolean;
|
|
8
|
+
maxHistory?: number;
|
|
9
|
+
level?: LogLevel[];
|
|
10
|
+
}
|
|
11
|
+
interface LogEntry {
|
|
12
|
+
id: string;
|
|
13
|
+
timestamp: string;
|
|
14
|
+
level: LogLevel;
|
|
15
|
+
message: string;
|
|
16
|
+
args: any[];
|
|
17
|
+
}
|
|
18
|
+
interface EventEmitter {
|
|
19
|
+
on(event: string, listener: Function): () => void;
|
|
20
|
+
emit(event: string, ...args: any[]): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
declare class SmartLogger {
|
|
24
|
+
#private;
|
|
25
|
+
static getConfig(): SmartLoggerConfig;
|
|
26
|
+
static init(config?: SmartLoggerConfig): void;
|
|
27
|
+
static getHistory(): LogEntry[];
|
|
28
|
+
static clearHistory(): void;
|
|
29
|
+
static destroy(): void;
|
|
30
|
+
static subscribeToLogs(callback: (log: LogEntry) => void): () => void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* SmartLoggerDevTools is the main component that combines the floating button and log modal.
|
|
35
|
+
* It should be placed at the root level of your React application.
|
|
36
|
+
*
|
|
37
|
+
* Usage:
|
|
38
|
+
* ```tsx
|
|
39
|
+
* import { SmartLoggerDevTools } from 'smart-logger-devtools';
|
|
40
|
+
*
|
|
41
|
+
* function App() {
|
|
42
|
+
* return (
|
|
43
|
+
* <>
|
|
44
|
+
* <YourApp />
|
|
45
|
+
* <SmartLoggerDevTools />
|
|
46
|
+
* </>
|
|
47
|
+
* );
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare const SmartLoggerDevTools: React.FC;
|
|
52
|
+
|
|
53
|
+
declare const useLogHistory: () => LogEntry[];
|
|
54
|
+
|
|
55
|
+
export { type EventEmitter, type LogEntry, type LogLevel, SmartLogger, type SmartLoggerConfig, SmartLoggerDevTools, useLogHistory };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type LogLevel = "log" | "warn" | "error" | "info" | "debug";
|
|
4
|
+
interface SmartLoggerConfig {
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
showTimestamp?: boolean;
|
|
7
|
+
showComponentName?: boolean;
|
|
8
|
+
maxHistory?: number;
|
|
9
|
+
level?: LogLevel[];
|
|
10
|
+
}
|
|
11
|
+
interface LogEntry {
|
|
12
|
+
id: string;
|
|
13
|
+
timestamp: string;
|
|
14
|
+
level: LogLevel;
|
|
15
|
+
message: string;
|
|
16
|
+
args: any[];
|
|
17
|
+
}
|
|
18
|
+
interface EventEmitter {
|
|
19
|
+
on(event: string, listener: Function): () => void;
|
|
20
|
+
emit(event: string, ...args: any[]): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
declare class SmartLogger {
|
|
24
|
+
#private;
|
|
25
|
+
static getConfig(): SmartLoggerConfig;
|
|
26
|
+
static init(config?: SmartLoggerConfig): void;
|
|
27
|
+
static getHistory(): LogEntry[];
|
|
28
|
+
static clearHistory(): void;
|
|
29
|
+
static destroy(): void;
|
|
30
|
+
static subscribeToLogs(callback: (log: LogEntry) => void): () => void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* SmartLoggerDevTools is the main component that combines the floating button and log modal.
|
|
35
|
+
* It should be placed at the root level of your React application.
|
|
36
|
+
*
|
|
37
|
+
* Usage:
|
|
38
|
+
* ```tsx
|
|
39
|
+
* import { SmartLoggerDevTools } from 'smart-logger-devtools';
|
|
40
|
+
*
|
|
41
|
+
* function App() {
|
|
42
|
+
* return (
|
|
43
|
+
* <>
|
|
44
|
+
* <YourApp />
|
|
45
|
+
* <SmartLoggerDevTools />
|
|
46
|
+
* </>
|
|
47
|
+
* );
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare const SmartLoggerDevTools: React.FC;
|
|
52
|
+
|
|
53
|
+
declare const useLogHistory: () => LogEntry[];
|
|
54
|
+
|
|
55
|
+
export { type EventEmitter, type LogEntry, type LogLevel, SmartLogger, type SmartLoggerConfig, SmartLoggerDevTools, useLogHistory };
|