-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (141 loc) · 3.74 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
const chalk = require('chalk');
/**
* Simple and pretty logging
* @example
* const me = require('pyx-me').create('MyLog');
*/
class PyxLog {
constructor(name) {
/**
* Name of the logger instance.
* @name PyxLog#_name
* @type {string}
* @instance
* @private
*/
this._name = name;
/**
* Mode of logger. Determines if logger is silent or not.
* @name PyxLog#_mode
* @type {number}
* @instance
* @default 1
* @private
*/
this._mode = 1;
/**
* The name of the logger wrapped with chalk.
* @name PyxLog#_prettyName
* @type {string}
* @instance
* @private
*/
this._prettyName = chalk.bold.blue(`[${this._name}]`);
}
/**
* This silences the logger; once this method is called,
* no more output will be coming from the logger until
* {@link beNormal} is called or the debug option is enabled
* @public
*/
beSilent() {
this._mode = 0;
}
/**
* This is the default state of the logger; This
* will set the logger back to normal _mode
* if it was originally put to silent _mode by calling
* {@link beSilent}
* @public
*/
beNormal() {
this._mode = 1;
}
/**
* Outputs a header me including the name of the logger.
* Good for breaks in logic or initializing messages.
* #note: Passed through the {@link _runThisInMode} method
* @param {string} msg - String that will be embedded in the header
* @public
*/
header(msg) {
this._runThisInMode(() => {
console.log(`${chalk.bold(`===> ${msg}`)} ${this._prettyName}`);
});
}
/**
* Outputs regular me messages.
* Good for data logs or simple messages.
* #note: Passed through the {@link _runThisInMode} method
* @param {...*} msg - Array of values (string or other) that will be printed out on the screen
* @public
*/
log(...msg) {
this._runThisInMode(() => {
if (msg.length > 1) {
console.log(`${this._prettyName} =>`);
for (const m of msg) {
console.log(` ${(typeof m === 'string') ? m : JSON.stringify(m)}`);
}
console.log(`${this._prettyName} <=`);
} else {
const m = msg[0];
console.log(` ${(typeof m === 'string') ? m : JSON.stringify(m)} ${this._prettyName}`);
}
});
}
/**
* Outputs debug me messages.
* Good for debugging of code while coding or as extra me information to debug reoccurring bugs.
* #note: This method only prints out if the `DEBUG_MODE` env variable is set and ignores _mode
* @param {...*} msg - Array of values (string or other) that will be printed out on the screen
* @public
*/
debug(...msg) {
if (typeof process.env.DEBUG_MODE !== 'undefined') {
const mainMsg = msg.shift();
console.log(`${chalk.bold.yellow('D')}${chalk.bold.white(` => ${mainMsg}`)} ${this._prettyName}`);
if (msg.length > 0) {
msg.forEach((m) => {
console.log(m);
});
console.log(`${chalk.bold.white(`===# `)}${chalk.bold.yellow('DEBUG')} ${this._prettyName}`);
}
}
}
/**
* Outputs alert me messages.
* Good for errors or warnings.
* #note: Passed through the {@link _runThisInMode} method
* @param {string} msg - String that will be embedded in the alert
* @public
*/
alert(msg) {
this._runThisInMode(() => {
console.log(`${chalk.bold.red('A')}${chalk.bold.white(` => ${msg} `)}${chalk.bold.red('!!!!')} ${this._prettyName}`);
});
}
/**
* Runs an action based on _mode
* @see _mode
* @param {function} actionCb - Function that will be called if on normal _mode (1)
* @private
*/
_runThisInMode(actionCb) {
if (this._mode === 1) {
actionCb();
}
}
/**
* Static instance generator.
* Creates a new instance of the PyxLog logger.
* @param {string} name - Name of the logger.
* @return {PyxLog}
* @public
* @static
*/
static create(name) {
return new PyxLog(name);
}
}
module.exports = PyxLog;