Skip to content

Commit c03b560

Browse files
committed
readme | updation function
1 parent 226f0b6 commit c03b560

File tree

4 files changed

+130
-12
lines changed

4 files changed

+130
-12
lines changed

README.md

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# js-dlogger (logger for js frameworks)
2+
3+
4+
## **📌 Description**
5+
6+
js-dlogger is a lightweight, configurable logger for JavaScript, React.js, and React Native applications. It provides structured logging with different levels (info, debug, dLog, Warn, Error) and supports environment-based logging.
7+
8+
---
9+
10+
11+
12+
## **Installation**
13+
14+
Install the package via npm:
15+
16+
```bash
17+
npm install js-dlogger
18+
19+
```
20+
21+
or
22+
```bash
23+
yarn add js-dlogger
24+
25+
```
26+
27+
## Usage :
28+
29+
## **✅ Basic Usage**
30+
Here’s an example to get you started:
31+
32+
```javascript
33+
import Logger from "js-dlogger";
34+
35+
Logger.info("App", "INIT", "Application started");
36+
Logger.debug("App", "DEBUG", "This is a debug message");
37+
Logger.dLog("App", "DATA", { key: "value" });
38+
Logger.warn("App", "WARNING", "This is a warning message");
39+
Logger.error("App", "ERROR", "An error occurred");
40+
```
41+
## **📌 Logger with File Context**
42+
Here’s an example to get you started:
43+
```javascript
44+
import {LoggerWithFile} from 'js-dlogger/loggerWrapper';
45+
46+
const Logger = LoggerWithFile("Login.jsx");
47+
Logger.info("INIT", "Application started");
48+
Logger.debug("DEBUG", "This is a debug message");
49+
Logger.dLog("DATA", { key: "value" });
50+
Logger.warn("WARNING", "This is a warning message");
51+
Logger.error("ERROR", "An error occurred");
52+
```
53+
54+
## **🌎 Environment-Based Logging**
55+
The logger dynamically determines the environment:
56+
- **React**: If globalThis.__APP_ENV__ is set, it will be used.
57+
- **React Native**: If logger.config.js is available, it loads the environment from there.
58+
- **Any js-framework** : Otherwise, it defaults to development.
59+
60+
**Example logger.config.js:**
61+
```javascript
62+
/**
63+
* Config file for {logger}
64+
* @return Object : ENV value
65+
* @alias : development | production
66+
* @development ~ for development environment
67+
* @production ~ release & disable debug logs
68+
* @author structlooper
69+
* */
70+
export default {
71+
ENV: 'production',
72+
};
73+
```
74+
## **🛠 Best Practices**
75+
**1️⃣ Ensure the Config File Exists (If Required)**
76+
77+
If your project needs logger.config.js, make sure to create it:
78+
```javascript
79+
export default {
80+
ENV: 'production',
81+
};
82+
```
83+
**2️⃣ Use File-Based Logging for Easier Debugging**
84+
85+
Instead of passing taq manually, use LoggerWithFile for consistency.
86+
```javascript
87+
const Logger = LoggerWithFile("Login.jsx")
88+
```
89+
90+
---
91+
92+
## **⚡️ Troubleshooting**
93+
94+
| Issue | Solution |
95+
|------------------------------------|-------------------------------------------------------------------------|
96+
| Metro bundler error | Ensure `logger.config.js` exists or use the `try-catch` approach. |
97+
| Environment is always development | Check if `globalThis.__APP_ENV__` or `logger.config.js` is correctly set. |
98+
---
99+
## **📜 License**
100+
101+
This project is licensed under the MIT License. See the [LICENSE](https://opensource.org/licenses/MIT) file for details.
102+
103+
---
104+
105+
## **Contributing**
106+
107+
Contributions, issues, and feature requests are welcome!
108+
Feel free to check the [issues page](https://github.com/structlooper/js-dlogger/issues).
109+
110+
---
111+
112+
## **📧 Contact**
113+
114+
For support, feedback, or contributions, reach out via GitHub: [structlooper](https://github.com/structlooper).
115+

logger.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ try {
66
config = require('../../logger.config')?.default;
77
} catch (error) {
88
console.warn(
9-
'No environment config found, defaulting to "development".\n ' +
10-
'If using React Native, create a file "logger.config.js" in the root directory'
9+
'No environment config found, defaulting to "development".\n ' +
10+
'If using React Native, create a file "logger.config.js" in the root directory'
1111
);
1212
}
1313

@@ -18,16 +18,16 @@ if (globalThis.__APP_ENV__) {
1818
environment = config.ENV;
1919
}
2020

21-
console.warn('Project Environment set to:', environment);
21+
console.log('Project Environment set to:', environment);
2222

2323
const isDev: boolean = (environment.toLowerCase() === "development");
2424

2525
interface Logger {
2626
info: (taq: string, indicator: string, message?: string) => void;
2727
debug: (taq: string, indicator: string, message?: string) => void;
2828
dLog: (taq: string, indicator: string, data?: any) => void;
29-
Warn: (taq: string, indicator: string, message?: string) => void;
30-
Error: (taq: string, indicator: string, message?: string) => void;
29+
warn: (taq: string, indicator: string, message?: string) => void;
30+
error: (taq: string, indicator: string, message?: string) => void;
3131
}
3232

3333
const Logger: Logger = {
@@ -47,10 +47,10 @@ const Logger: Logger = {
4747
console.log('---------------------------------\n');
4848
}
4949
},
50-
Warn: (taq: string, indicator: string, message: string = "UNIVERSAL") => {
50+
warn: (taq: string, indicator: string, message: string = "UNIVERSAL") => {
5151
console.warn(`[WARN][${taq}][${indicator}]`, message);
5252
},
53-
Error: (taq: string, indicator: string, message: string = "UNIVERSAL") => {
53+
error: (taq: string, indicator: string, message: string = "UNIVERSAL") => {
5454
console.error(`[ERROR][${taq}][${indicator}]`, message);
5555
}
5656
};

loggerWrapper.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import Logger from "logger/logger";
1+
import Logger from "./logger";
22

33
const loggerWrapper = (loggerFunction, fileName) => (...args) => loggerFunction(fileName, ...args);
44
export const LoggerWithFile = (fileName) => {
55
return {
66
info: loggerWrapper(Logger.info, fileName),
77
debug: loggerWrapper(Logger.debug, fileName),
88
dLog: loggerWrapper(Logger.dLog, fileName),
9-
Warn: loggerWrapper(Logger.Warn, fileName),
10-
Error: loggerWrapper(Logger.Error, fileName),
9+
warn: loggerWrapper(Logger.warn, fileName),
10+
error: loggerWrapper(Logger.error, fileName),
1111
};
1212
};
1313

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-dlogger",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A lightweight and efficient logging utility for JavaScript applications. This package provides multiple logging functions such as info(), dLog(), warn(), debug and error(), displaying logs in the console along with the filename for better debugging and traceability. Perfect for developers looking for a simple yet powerful logging solution.",
55
"main": "logger.ts",
66
"scripts": {
@@ -23,7 +23,10 @@
2323
"node-logger",
2424
"nextjs-logger"
2525
],
26-
"author": "structlooper",
26+
"author": {
27+
"name" : "Deepak Kumar",
28+
"url" : "https://github.com/structlooper"
29+
},
2730
"license": "MIT",
2831
"bugs": {
2932
"url": "https://github.com/structlooper/js-dlogger/issues"

0 commit comments

Comments
 (0)