-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
162 lines (143 loc) · 2.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const colors = require("colors");
const fs = require("fs");
/**
* `setColor` method is used for setting colors
*
* @param {boolean} color - it accepts a boolean value
*/
const setColor = (color = true) => (color ? colors.enable() : colors.disable());
//setting theme
colors.setTheme({
silly: "rainbow",
verbose: "cyan",
prompt: "grey",
info: "green",
warn: "yellow",
debug: "blue",
error: "red",
});
/** flag for allowing to log in a file */
let fileLog = false;
let logStream;
/**
* used for logging your terminal output in `ps-logger.log` file
*
* use this method only once it will work globally to output all your logs in log file
*
* @param {boolean} fileLogging - it accepts a boolean value
*
* @param {string} fileName - it accepts a string value for file name,don't add extensions
*
* @default - false
*
*/
const logToFile = (fileLogging = false, fileName = "ps-logger") => {
if (fileLogging) {
// Create a writable stream to the log file
logStream = fs.createWriteStream(`./${fileName}.log`, { flags: "a" });
fileLog = true;
setColor(false);
}
};
/** core method to log in console and file */
const log = (args) => {
process.stdout.write(args + "\n");
if (fileLog) {
logStream.write(args + "\n");
}
};
const getDate = () => {
//setting time in (HH:MM:SS)
const date = new Date();
const hours = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
return `${hours}:${minute}:${second}`;
};
/**
* used for logging info messages
*
* @param {any} arg
*/
const info = (arg) => {
log(
colors.bold.green("[info]:") + colors.bold.gray(getDate()) + ` ${arg}`.info
);
};
/**
* used for logging warn messages
*
* @param {any} arg
*/
const warn = (arg) => {
log(
colors.bold.yellow("[warn]:") + colors.bold.gray(getDate()) + ` ${arg}`.warn
);
};
/**
* used for logging error messages
*
* @param {any} arg
*/
const error = (arg) => {
log(
colors.bold.red("[error]:") + colors.bold.gray(getDate()) + ` ${arg}`.error
);
};
/**
* used for logging debug messages
*
* @param {any} arg
*/
const debug = (arg) => {
log(
colors.bold.blue("[debug]:") + colors.bold.gray(getDate()) + ` ${arg}`.debug
);
};
/**
* used for logging silly messages
*
* @param {any} arg
*/
const silly = (arg) => {
log(
colors.rainbow("[silly]:") + colors.bold.gray(getDate()) + ` ${arg}`.silly
);
};
/**
* used for logging verbose messages
*
* @param {any} arg
*/
const verbose = (arg) => {
log(
colors.bold.cyan("[verbose]:") +
colors.bold.gray(getDate()) +
` ${arg}`.verbose
);
};
/**
* used for logging prompt messages
*
* @param {any} arg
*/
const prompt = (arg) => {
log(
colors.bold.grey("[prompt]:") +
colors.bold.gray(getDate()) +
` ${arg}`.prompt
);
};
// Close the log stream when you are done
// logStream.end();
module.exports = {
info,
warn,
error,
debug,
silly,
verbose,
prompt,
setColor,
logToFile,
};