@rdrudra99/hardlog 0.1.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 +21 -0
- package/README.md +342 -0
- package/dist/env.d.ts +21 -0
- package/dist/env.js +68 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.js +53 -0
- package/dist/logger.d.ts +50 -0
- package/dist/logger.js +156 -0
- package/dist/types.d.ts +23 -0
- package/dist/types.js +2 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rdrudra99
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
# @rdrudra99/hardlog
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@rdrudra99/hardlog)
|
|
4
|
+
[](https://www.npmjs.com/package/@rdrudra99/hardlog)
|
|
5
|
+
[](https://github.com/Rdrudra99/hardlog/blob/main/LICENSE)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
|
|
8
|
+
Beautiful, colorful dev-only logging for Node.js and Browser with **zero configuration**.
|
|
9
|
+
|
|
10
|
+
Automatically detects your runtime environment and applies appropriate styling:
|
|
11
|
+
- 🎨 **Node.js**: ANSI colors in terminal
|
|
12
|
+
- 🌐 **Browser**: CSS-styled console messages
|
|
13
|
+
- 🔒 **Production-safe**: Automatically disabled in production
|
|
14
|
+
- 🚀 **Zero dependencies**: No external libraries needed
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
bun add @rdrudra99/hardlog
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install @rdrudra99/hardlog
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
yarn add @rdrudra99/hardlog
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm add @rdrudra99/hardlog
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Basic Usage
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import log from '@rdrudra99/hardlog';
|
|
38
|
+
|
|
39
|
+
log.success('Server started successfully!');
|
|
40
|
+
log.error('Database connection failed');
|
|
41
|
+
log.warn('Missing environment variable');
|
|
42
|
+
log.info('Listening on port 3000');
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Output in Node.js Terminal
|
|
46
|
+
```
|
|
47
|
+
✔ SUCCESS Server started successfully!
|
|
48
|
+
✖ ERROR Database connection failed
|
|
49
|
+
⚠ WARNING Missing environment variable
|
|
50
|
+
ℹ INFO Listening on port 3000
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Output in Browser DevTools
|
|
54
|
+

|
|
55
|
+
|
|
56
|
+
## Examples
|
|
57
|
+
|
|
58
|
+
### Node.js / Express
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import express from 'express';
|
|
62
|
+
import log from '@rdrudra99/hardlog';
|
|
63
|
+
|
|
64
|
+
const app = express();
|
|
65
|
+
const PORT = 3000;
|
|
66
|
+
|
|
67
|
+
app.get('/', (req, res) => {
|
|
68
|
+
log.info(`${req.method} ${req.path}`);
|
|
69
|
+
res.send('Hello World!');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
app.listen(PORT, () => {
|
|
73
|
+
log.success(`Server running on http://localhost:${PORT}`);
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Next.js - App Router (Server Component)
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
// app/page.tsx
|
|
81
|
+
import log from '@rdrudra99/hardlog';
|
|
82
|
+
|
|
83
|
+
export default async function HomePage() {
|
|
84
|
+
// This runs on the server
|
|
85
|
+
log.info('Rendering home page');
|
|
86
|
+
|
|
87
|
+
const data = await fetchData();
|
|
88
|
+
log.success('Data fetched successfully');
|
|
89
|
+
|
|
90
|
+
return <div>{data.title}</div>;
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Next.js - App Router (Client Component)
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
'use client';
|
|
98
|
+
|
|
99
|
+
import { useEffect } from 'react';
|
|
100
|
+
import log from '@rdrudra99/hardlog';
|
|
101
|
+
|
|
102
|
+
export default function ClientComponent() {
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
// This runs in the browser
|
|
105
|
+
log.success('Client component mounted');
|
|
106
|
+
|
|
107
|
+
return () => {
|
|
108
|
+
log.info('Client component unmounted');
|
|
109
|
+
};
|
|
110
|
+
}, []);
|
|
111
|
+
|
|
112
|
+
const handleClick = () => {
|
|
113
|
+
log.info('Button clicked');
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return <button onClick={handleClick}>Click me</button>;
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Next.js - Pages Router
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
// pages/index.tsx
|
|
124
|
+
import { GetServerSideProps } from 'next';
|
|
125
|
+
import log from '@rdrudra99/hardlog';
|
|
126
|
+
|
|
127
|
+
export const getServerSideProps: GetServerSideProps = async () => {
|
|
128
|
+
log.info('Fetching data for page');
|
|
129
|
+
|
|
130
|
+
const data = await fetchData();
|
|
131
|
+
log.success('Data loaded');
|
|
132
|
+
|
|
133
|
+
return { props: { data } };
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export default function Page({ data }) {
|
|
137
|
+
return <div>{data.title}</div>;
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Browser-Only App
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
import log from '@rdrudra99/hardlog';
|
|
145
|
+
|
|
146
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
147
|
+
log.success('DOM loaded');
|
|
148
|
+
|
|
149
|
+
const button = document.querySelector('#myButton');
|
|
150
|
+
button?.addEventListener('click', () => {
|
|
151
|
+
log.info('Button clicked');
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## API Reference
|
|
157
|
+
|
|
158
|
+
### `log.success(message: string): void`
|
|
159
|
+
Log a success message with green styling.
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
log.success('Operation completed successfully');
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### `log.error(message: string): void`
|
|
166
|
+
Log an error message with red styling.
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
log.error('Failed to connect to database');
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### `log.warn(message: string): void`
|
|
173
|
+
Log a warning message with yellow/orange styling.
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
log.warn('API key is missing');
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### `log.info(message: string): void`
|
|
180
|
+
Log an info message with blue styling.
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
log.info('Server is starting...');
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### `log.config(options: LoggerConfig): typeof log`
|
|
187
|
+
Configure logger options. Returns the log object for chaining.
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
log.config({
|
|
191
|
+
enabled: true,
|
|
192
|
+
showTimestamp: true
|
|
193
|
+
});
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
#### Configuration Options
|
|
197
|
+
|
|
198
|
+
| Option | Type | Default | Description |
|
|
199
|
+
|--------|------|---------|-------------|
|
|
200
|
+
| `enabled` | `boolean` | `true` in dev, `false` in production | Enable or disable logging |
|
|
201
|
+
| `showTimestamp` | `boolean` | `false` | Show timestamp in log messages |
|
|
202
|
+
|
|
203
|
+
### Configuration Examples
|
|
204
|
+
|
|
205
|
+
#### Enable timestamps
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
import log from '@rdrudra99/hardlog';
|
|
209
|
+
|
|
210
|
+
log.config({ showTimestamp: true });
|
|
211
|
+
|
|
212
|
+
log.info('Server started');
|
|
213
|
+
// Output: ℹ INFO [10:30:45 AM] Server started
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
#### Force enable in production (NOT RECOMMENDED)
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
import log from '@rdrudra99/hardlog';
|
|
220
|
+
|
|
221
|
+
// ⚠️ Only do this if you understand the implications
|
|
222
|
+
log.config({ enabled: true });
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
#### Chain configuration
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
import log from '@rdrudra99/hardlog';
|
|
229
|
+
|
|
230
|
+
log
|
|
231
|
+
.config({ showTimestamp: true })
|
|
232
|
+
.success('Configured and ready!');
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## ⚠️ Dev-Only Warning
|
|
236
|
+
|
|
237
|
+
**This package is designed for development use only.**
|
|
238
|
+
|
|
239
|
+
By default, all logging is **automatically disabled** when `NODE_ENV === 'production'`.
|
|
240
|
+
|
|
241
|
+
This ensures:
|
|
242
|
+
- No performance overhead in production
|
|
243
|
+
- No sensitive information leaks
|
|
244
|
+
- Clean production logs
|
|
245
|
+
- Zero impact on bundle size behavior
|
|
246
|
+
|
|
247
|
+
### How it works
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
// Automatically disabled in production
|
|
251
|
+
process.env.NODE_ENV = 'production';
|
|
252
|
+
|
|
253
|
+
log.info('This will NOT appear'); // Silent in production
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
To explicitly enable in production (not recommended):
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
log.config({ enabled: true });
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## Features
|
|
263
|
+
|
|
264
|
+
✅ **Zero Configuration** - Works immediately after install
|
|
265
|
+
✅ **Environment Detection** - Auto-detects Node.js vs Browser
|
|
266
|
+
✅ **Production Safe** - Disabled by default in production
|
|
267
|
+
✅ **TypeScript Support** - Full type definitions included
|
|
268
|
+
✅ **Zero Dependencies** - No external packages
|
|
269
|
+
✅ **Lightweight** - Minimal footprint
|
|
270
|
+
✅ **SSR Compatible** - Works with Next.js, Remix, etc.
|
|
271
|
+
✅ **Edge Runtime Safe** - Won't crash in Vercel, Cloudflare Workers
|
|
272
|
+
✅ **Never Throws** - Fails silently, never breaks your app
|
|
273
|
+
|
|
274
|
+
## Requirements
|
|
275
|
+
|
|
276
|
+
- Node.js >= 14.0.0
|
|
277
|
+
- Bun >= 1.0.0
|
|
278
|
+
- Modern browser with console support
|
|
279
|
+
|
|
280
|
+
## TypeScript
|
|
281
|
+
|
|
282
|
+
This package is written in TypeScript and ships with type definitions.
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
import log, { LoggerConfig } from '@rdrudra99/hardlog';
|
|
286
|
+
|
|
287
|
+
const config: LoggerConfig = {
|
|
288
|
+
enabled: true,
|
|
289
|
+
showTimestamp: false,
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
log.config(config);
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## Browser Support
|
|
296
|
+
|
|
297
|
+
Works in all modern browsers that support:
|
|
298
|
+
- `console.log` with CSS styling (`%c`)
|
|
299
|
+
- ES2018 features
|
|
300
|
+
|
|
301
|
+
Tested in:
|
|
302
|
+
- Chrome/Edge (latest)
|
|
303
|
+
- Firefox (latest)
|
|
304
|
+
- Safari (latest)
|
|
305
|
+
|
|
306
|
+
## Node.js Support
|
|
307
|
+
|
|
308
|
+
Works in:
|
|
309
|
+
- Bun (recommended)
|
|
310
|
+
- Node.js 14+
|
|
311
|
+
- Deno (with npm compatibility)
|
|
312
|
+
|
|
313
|
+
## Contributing
|
|
314
|
+
|
|
315
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
316
|
+
|
|
317
|
+
## License
|
|
318
|
+
|
|
319
|
+
MIT © Rdrudra99
|
|
320
|
+
|
|
321
|
+
## Related Projects
|
|
322
|
+
|
|
323
|
+
- [chalk](https://github.com/chalk/chalk) - Terminal string styling
|
|
324
|
+
- [consola](https://github.com/unjs/consola) - Elegant Console Logger
|
|
325
|
+
- [pino](https://github.com/pinojs/pino) - Super fast logger
|
|
326
|
+
|
|
327
|
+
## Why Another Logger?
|
|
328
|
+
|
|
329
|
+
Most logging libraries are either:
|
|
330
|
+
- Too heavy (large dependencies)
|
|
331
|
+
- Node.js only
|
|
332
|
+
- Browser only
|
|
333
|
+
- Require configuration
|
|
334
|
+
- Not production-safe by default
|
|
335
|
+
|
|
336
|
+
`@rdrudra99/hardlog` is designed specifically for the **developer experience** during development, with:
|
|
337
|
+
- Automatic environment detection
|
|
338
|
+
- Zero configuration
|
|
339
|
+
- Production safety built-in
|
|
340
|
+
- Works everywhere (Node + Browser)
|
|
341
|
+
|
|
342
|
+
Perfect for quick debugging, prototyping, and development workflows.
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { RuntimeEnvironment } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Safely detect if we're in a browser environment
|
|
4
|
+
*/
|
|
5
|
+
export declare function isBrowser(): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Safely detect if we're in a Node.js environment
|
|
8
|
+
*/
|
|
9
|
+
export declare function isNode(): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Detect the current runtime environment
|
|
12
|
+
*/
|
|
13
|
+
export declare function detectEnvironment(): RuntimeEnvironment;
|
|
14
|
+
/**
|
|
15
|
+
* Check if we're in production mode
|
|
16
|
+
*/
|
|
17
|
+
export declare function isProduction(): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Check if logging should be enabled by default
|
|
20
|
+
*/
|
|
21
|
+
export declare function shouldLogByDefault(): boolean;
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isBrowser = isBrowser;
|
|
4
|
+
exports.isNode = isNode;
|
|
5
|
+
exports.detectEnvironment = detectEnvironment;
|
|
6
|
+
exports.isProduction = isProduction;
|
|
7
|
+
exports.shouldLogByDefault = shouldLogByDefault;
|
|
8
|
+
/**
|
|
9
|
+
* Safely detect if we're in a browser environment
|
|
10
|
+
*/
|
|
11
|
+
function isBrowser() {
|
|
12
|
+
try {
|
|
13
|
+
return typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
14
|
+
}
|
|
15
|
+
catch (_a) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Safely detect if we're in a Node.js environment
|
|
21
|
+
*/
|
|
22
|
+
function isNode() {
|
|
23
|
+
try {
|
|
24
|
+
return (typeof process !== 'undefined' &&
|
|
25
|
+
process.versions != null &&
|
|
26
|
+
process.versions.node != null);
|
|
27
|
+
}
|
|
28
|
+
catch (_a) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Detect the current runtime environment
|
|
34
|
+
*/
|
|
35
|
+
function detectEnvironment() {
|
|
36
|
+
try {
|
|
37
|
+
if (isBrowser()) {
|
|
38
|
+
return 'browser';
|
|
39
|
+
}
|
|
40
|
+
if (isNode()) {
|
|
41
|
+
return 'node';
|
|
42
|
+
}
|
|
43
|
+
return 'unknown';
|
|
44
|
+
}
|
|
45
|
+
catch (_a) {
|
|
46
|
+
return 'unknown';
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Check if we're in production mode
|
|
51
|
+
*/
|
|
52
|
+
function isProduction() {
|
|
53
|
+
try {
|
|
54
|
+
if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV) {
|
|
55
|
+
return process.env.NODE_ENV === 'production';
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
catch (_a) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Check if logging should be enabled by default
|
|
65
|
+
*/
|
|
66
|
+
function shouldLogByDefault() {
|
|
67
|
+
return !isProduction();
|
|
68
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { LoggerConfig } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Dev-only logger with beautiful, colorful output
|
|
4
|
+
*
|
|
5
|
+
* Automatically detects Node.js or Browser environment and applies appropriate styling.
|
|
6
|
+
* Disabled by default in production (NODE_ENV === 'production').
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import log from '@rdrudra99/hardlog';
|
|
11
|
+
*
|
|
12
|
+
* log.success('Server started successfully!');
|
|
13
|
+
* log.error('Database connection failed');
|
|
14
|
+
* log.warn('Missing environment variable');
|
|
15
|
+
* log.info('Listening on port 3000');
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* // Configure logger
|
|
21
|
+
* import log from '@rdrudra99/hardlog';
|
|
22
|
+
*
|
|
23
|
+
* log.config({ enabled: true, showTimestamp: true });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
declare const log: {
|
|
27
|
+
/**
|
|
28
|
+
* Log a success message
|
|
29
|
+
*/
|
|
30
|
+
success: (message: string) => void;
|
|
31
|
+
/**
|
|
32
|
+
* Log an error message
|
|
33
|
+
*/
|
|
34
|
+
error: (message: string) => void;
|
|
35
|
+
/**
|
|
36
|
+
* Log a warning message
|
|
37
|
+
*/
|
|
38
|
+
warn: (message: string) => void;
|
|
39
|
+
/**
|
|
40
|
+
* Log an info message
|
|
41
|
+
*/
|
|
42
|
+
info: (message: string) => void;
|
|
43
|
+
/**
|
|
44
|
+
* Configure logger options
|
|
45
|
+
*/
|
|
46
|
+
config: (options: LoggerConfig) => /*elided*/ any;
|
|
47
|
+
};
|
|
48
|
+
export default log;
|
|
49
|
+
export type { LoggerConfig };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const logger_1 = require("./logger");
|
|
4
|
+
/**
|
|
5
|
+
* Dev-only logger with beautiful, colorful output
|
|
6
|
+
*
|
|
7
|
+
* Automatically detects Node.js or Browser environment and applies appropriate styling.
|
|
8
|
+
* Disabled by default in production (NODE_ENV === 'production').
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import log from '@rdrudra99/hardlog';
|
|
13
|
+
*
|
|
14
|
+
* log.success('Server started successfully!');
|
|
15
|
+
* log.error('Database connection failed');
|
|
16
|
+
* log.warn('Missing environment variable');
|
|
17
|
+
* log.info('Listening on port 3000');
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* // Configure logger
|
|
23
|
+
* import log from '@rdrudra99/hardlog';
|
|
24
|
+
*
|
|
25
|
+
* log.config({ enabled: true, showTimestamp: true });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
const log = {
|
|
29
|
+
/**
|
|
30
|
+
* Log a success message
|
|
31
|
+
*/
|
|
32
|
+
success: (message) => logger_1.logger.success(message),
|
|
33
|
+
/**
|
|
34
|
+
* Log an error message
|
|
35
|
+
*/
|
|
36
|
+
error: (message) => logger_1.logger.error(message),
|
|
37
|
+
/**
|
|
38
|
+
* Log a warning message
|
|
39
|
+
*/
|
|
40
|
+
warn: (message) => logger_1.logger.warn(message),
|
|
41
|
+
/**
|
|
42
|
+
* Log an info message
|
|
43
|
+
*/
|
|
44
|
+
info: (message) => logger_1.logger.info(message),
|
|
45
|
+
/**
|
|
46
|
+
* Configure logger options
|
|
47
|
+
*/
|
|
48
|
+
config: (options) => {
|
|
49
|
+
logger_1.logger.configure(options);
|
|
50
|
+
return log;
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
exports.default = log;
|
package/dist/logger.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { LoggerConfig } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Logger class - handles all logging operations
|
|
4
|
+
*/
|
|
5
|
+
declare class Logger {
|
|
6
|
+
private config;
|
|
7
|
+
private runtime;
|
|
8
|
+
constructor();
|
|
9
|
+
/**
|
|
10
|
+
* Configure logger options
|
|
11
|
+
*/
|
|
12
|
+
configure(options: LoggerConfig): this;
|
|
13
|
+
/**
|
|
14
|
+
* Get current timestamp string
|
|
15
|
+
*/
|
|
16
|
+
private getTimestamp;
|
|
17
|
+
/**
|
|
18
|
+
* Log message in Node.js with ANSI colors
|
|
19
|
+
*/
|
|
20
|
+
private logNode;
|
|
21
|
+
/**
|
|
22
|
+
* Log message in browser with CSS styling
|
|
23
|
+
*/
|
|
24
|
+
private logBrowser;
|
|
25
|
+
/**
|
|
26
|
+
* Core log method - routes to appropriate handler
|
|
27
|
+
*/
|
|
28
|
+
private log;
|
|
29
|
+
/**
|
|
30
|
+
* Log success message
|
|
31
|
+
*/
|
|
32
|
+
success(message: string): void;
|
|
33
|
+
/**
|
|
34
|
+
* Log error message
|
|
35
|
+
*/
|
|
36
|
+
error(message: string): void;
|
|
37
|
+
/**
|
|
38
|
+
* Log warning message
|
|
39
|
+
*/
|
|
40
|
+
warn(message: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* Log info message
|
|
43
|
+
*/
|
|
44
|
+
info(message: string): void;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Singleton logger instance
|
|
48
|
+
*/
|
|
49
|
+
export declare const logger: Logger;
|
|
50
|
+
export {};
|
package/dist/logger.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logger = void 0;
|
|
4
|
+
const env_1 = require("./env");
|
|
5
|
+
/**
|
|
6
|
+
* ANSI color codes for Node.js terminal
|
|
7
|
+
*/
|
|
8
|
+
const ANSI_COLORS = {
|
|
9
|
+
reset: '\x1b[0m',
|
|
10
|
+
bright: '\x1b[1m',
|
|
11
|
+
green: '\x1b[32m',
|
|
12
|
+
red: '\x1b[31m',
|
|
13
|
+
yellow: '\x1b[33m',
|
|
14
|
+
blue: '\x1b[36m',
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* CSS styles for browser console
|
|
18
|
+
*/
|
|
19
|
+
const BROWSER_STYLES = {
|
|
20
|
+
success: 'background: #10b981; color: white; padding: 2px 6px; border-radius: 3px; font-weight: bold;',
|
|
21
|
+
error: 'background: #ef4444; color: white; padding: 2px 6px; border-radius: 3px; font-weight: bold;',
|
|
22
|
+
warn: 'background: #f59e0b; color: white; padding: 2px 6px; border-radius: 3px; font-weight: bold;',
|
|
23
|
+
info: 'background: #3b82f6; color: white; padding: 2px 6px; border-radius: 3px; font-weight: bold;',
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Log level symbols and labels
|
|
27
|
+
*/
|
|
28
|
+
const LOG_CONFIG = {
|
|
29
|
+
success: { symbol: '✔', label: 'SUCCESS', color: ANSI_COLORS.green },
|
|
30
|
+
error: { symbol: '✖', label: 'ERROR', color: ANSI_COLORS.red },
|
|
31
|
+
warn: { symbol: '⚠', label: 'WARNING', color: ANSI_COLORS.yellow },
|
|
32
|
+
info: { symbol: 'ℹ', label: 'INFO', color: ANSI_COLORS.blue },
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Logger class - handles all logging operations
|
|
36
|
+
*/
|
|
37
|
+
class Logger {
|
|
38
|
+
constructor() {
|
|
39
|
+
this.runtime = (0, env_1.detectEnvironment)();
|
|
40
|
+
this.config = {
|
|
41
|
+
enabled: (0, env_1.shouldLogByDefault)(),
|
|
42
|
+
showTimestamp: false,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Configure logger options
|
|
47
|
+
*/
|
|
48
|
+
configure(options) {
|
|
49
|
+
try {
|
|
50
|
+
if (options.enabled !== undefined) {
|
|
51
|
+
this.config.enabled = options.enabled;
|
|
52
|
+
}
|
|
53
|
+
if (options.showTimestamp !== undefined) {
|
|
54
|
+
this.config.showTimestamp = options.showTimestamp;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch (_a) {
|
|
58
|
+
// Fail silently
|
|
59
|
+
}
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get current timestamp string
|
|
64
|
+
*/
|
|
65
|
+
getTimestamp() {
|
|
66
|
+
try {
|
|
67
|
+
const now = new Date();
|
|
68
|
+
return `[${now.toLocaleTimeString()}]`;
|
|
69
|
+
}
|
|
70
|
+
catch (_a) {
|
|
71
|
+
return '';
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Log message in Node.js with ANSI colors
|
|
76
|
+
*/
|
|
77
|
+
logNode(level, message) {
|
|
78
|
+
try {
|
|
79
|
+
const cfg = LOG_CONFIG[level];
|
|
80
|
+
const timestamp = this.config.showTimestamp ? `${this.getTimestamp()} ` : '';
|
|
81
|
+
const formattedMessage = `${cfg.color}${cfg.symbol} ${cfg.label.padEnd(10)}${ANSI_COLORS.reset} ${timestamp}${message}`;
|
|
82
|
+
console.log(formattedMessage);
|
|
83
|
+
}
|
|
84
|
+
catch (_a) {
|
|
85
|
+
// Fail silently
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Log message in browser with CSS styling
|
|
90
|
+
*/
|
|
91
|
+
logBrowser(level, message) {
|
|
92
|
+
try {
|
|
93
|
+
const cfg = LOG_CONFIG[level];
|
|
94
|
+
const timestamp = this.config.showTimestamp ? `${this.getTimestamp()} ` : '';
|
|
95
|
+
const label = `${cfg.symbol} ${cfg.label}`;
|
|
96
|
+
console.log(`%c${label}%c ${timestamp}${message}`, BROWSER_STYLES[level], '');
|
|
97
|
+
}
|
|
98
|
+
catch (_a) {
|
|
99
|
+
// Fail silently
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Core log method - routes to appropriate handler
|
|
104
|
+
*/
|
|
105
|
+
log(level, message) {
|
|
106
|
+
// Don't log if disabled
|
|
107
|
+
if (!this.config.enabled) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
// Convert message to string safely
|
|
112
|
+
const msg = String(message !== null && message !== void 0 ? message : '');
|
|
113
|
+
if (this.runtime === 'node') {
|
|
114
|
+
this.logNode(level, msg);
|
|
115
|
+
}
|
|
116
|
+
else if (this.runtime === 'browser') {
|
|
117
|
+
this.logBrowser(level, msg);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
// Fallback to plain console.log
|
|
121
|
+
console.log(`[${level.toUpperCase()}]`, msg);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
catch (_a) {
|
|
125
|
+
// Fail silently - never break user's app
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Log success message
|
|
130
|
+
*/
|
|
131
|
+
success(message) {
|
|
132
|
+
this.log('success', message);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Log error message
|
|
136
|
+
*/
|
|
137
|
+
error(message) {
|
|
138
|
+
this.log('error', message);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Log warning message
|
|
142
|
+
*/
|
|
143
|
+
warn(message) {
|
|
144
|
+
this.log('warn', message);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Log info message
|
|
148
|
+
*/
|
|
149
|
+
info(message) {
|
|
150
|
+
this.log('info', message);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Singleton logger instance
|
|
155
|
+
*/
|
|
156
|
+
exports.logger = new Logger();
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for the logger
|
|
3
|
+
*/
|
|
4
|
+
export interface LoggerConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Enable or disable logging
|
|
7
|
+
* @default true in development, false in production
|
|
8
|
+
*/
|
|
9
|
+
enabled?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Show timestamp in logs
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
showTimestamp?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Runtime environment type
|
|
18
|
+
*/
|
|
19
|
+
export type RuntimeEnvironment = 'node' | 'browser' | 'unknown';
|
|
20
|
+
/**
|
|
21
|
+
* Log level type
|
|
22
|
+
*/
|
|
23
|
+
export type LogLevel = 'success' | 'error' | 'warn' | 'info';
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rdrudra99/hardlog",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Beautiful, colorful dev-only logging for Node.js and Browser with zero configuration",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md",
|
|
10
|
+
"LICENSE"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"prepublishOnly": "bun run build",
|
|
15
|
+
"watch": "tsc --watch",
|
|
16
|
+
"dev": "tsc --watch",
|
|
17
|
+
"test": "bun test-manual.js",
|
|
18
|
+
"clean": "rm -rf dist/"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"logger",
|
|
22
|
+
"logging",
|
|
23
|
+
"dev",
|
|
24
|
+
"development",
|
|
25
|
+
"console",
|
|
26
|
+
"color",
|
|
27
|
+
"colors",
|
|
28
|
+
"pretty",
|
|
29
|
+
"terminal",
|
|
30
|
+
"browser",
|
|
31
|
+
"node",
|
|
32
|
+
"typescript",
|
|
33
|
+
"bun",
|
|
34
|
+
"nextjs",
|
|
35
|
+
"express",
|
|
36
|
+
"debug",
|
|
37
|
+
"ansi",
|
|
38
|
+
"styled",
|
|
39
|
+
"zero-config",
|
|
40
|
+
"dev-tools"
|
|
41
|
+
],
|
|
42
|
+
"author": "Rdrudra99",
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/Rdrudra99/hardlog.git"
|
|
47
|
+
},
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/Rdrudra99/hardlog/issues"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/Rdrudra99/hardlog#readme",
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@types/node": "^25.0.3",
|
|
54
|
+
"typescript": "^5.3.3"
|
|
55
|
+
},
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": ">=14.0.0"
|
|
58
|
+
}
|
|
59
|
+
}
|