-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (90 loc) · 2.24 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
const fs = require('fs');
const LOG_DIR = 'logs';
const COLORS =
{
info: '\x1b[32m%s\x1b[0m',
error: '\x1b[31m%s\x1b[0m',
warning: '\x1b[33m%s\x1b[0m',
fatal: '\x1b[35m%s\x1b[0m'
}
/**
* This moduel is a wrapper for logging
*
* @param {mixed} data
* @return void
*/
module.exports =
{
/**
* Log additional info
*
* @param {string} data
* @return {void}
*/
info: function( data, options = {} )
{
this.loggString( 'INFO', data, COLORS.info, options );
},
/**
* Log Errors
*
* @param {string} data
* @return {void}
*/
error: function( data, options = {} )
{
this.loggString( 'ERROR', data, COLORS.error, options );
},
/**
* Log Warnings
*
* @param {string} data
* @return {void}
*/
warn: function( data, options = {} )
{
this.loggString( 'WARNING', data, COLORS.warning, options );
},
/**
* Log Fatal error
*
* @param {string} data
* @return {void}
*/
fatal: function( data, options = {} )
{
this.loggString( 'FATAL', data, COLORS.fatal, options );
},
/**
* Execute logging
*
* @param {string} data
* @param {string} label
* @return {void}
*/
loggString: function( label, data, color, options )
{
var d, str, line, filePath;
options.console = ( typeof options.console === 'undefined' ? true : options.console );
options.file = ( typeof options.file === 'undefined' ? true : options.file );
if( typeof data === 'object' )
data = JSON.stringify(data);
d = new Date();
str = '';
line = '\n/*************** ' + label.toUpperCase() + ' At ' + d + ' miliseconds : ' + d.getMilliseconds() + ' ***************/\n';
str += line;
str += data;
str += line;
if( options.file )
{
if ( !fs.existsSync(`./${LOG_DIR}`) )
{
fs.mkdirSync(`./${LOG_DIR}`);
}
filePath = `/${LOG_DIR}/` + label.toLowerCase() + `.log`;
fs.appendFileSync( __dirname + filePath, str );
}
if( options.console )
console.log( color, data );
}
}