-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.js
77 lines (69 loc) · 2.63 KB
/
log.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
"use strict";
/**
* @name Log.js
* @description A logger that you can utilise within your projects
* @author FantasyIsBae
* @license Apache-2.0
*/
const color = require("colors"),
fs = require("fs"),
request = require("minirequest"),
text = "",
log = {
err: function (message) {
console.log(Time() + "[" + "ERROR".red + "]", message.red);
},
success: function (message) {
console.log(Time() + "[" + "OK".green + "]", message.green);
},
warn: function (message) {
console.log(Time() + "[" + "WARNING".yellow + "]", message.yellow);
},
info: function (message) {
console.log(Time() + "[" + "INFO".blue + "]", message.blue);
},
logprint: function (message) {
console.log(Time() + "\b", message);
},
message: function (url) {
request(url, function (error, response, content) {
console.log(Time() + "[" + "MESSAGE".magenta + "]", content);
});
},
welcome: function () {
if (fs.existsSync("./package.json" || "../package.json")) {
var pjson = require("../../package.json");
project_version = pjson.version;
project_name = pjson.name;
this.info("Welcome to " + project_name.rainbow.underline + " version ".blue + project_version.rainbow.underline);
} else if (!fs.existsSync("./package.json" || "../package.json")) {
throw new TypeError("Package.json not found within this project.")
};
},
writeFile: function (message) {
text += message + "\n";
if (!fs.existsSync("./log")) {
this.err("./log doesn't exist!");
this.success("Creating new ./log folder.")
if (fs.existsSync("./package.json" || "../package.json")) {
var pjson = require("./package.json" || "../package.json");
project_name = pjson.name;
this.info("Please run " + project_name + " again for your log to save");
} else {
this.info("Please run your project again for your log to save");
}
fs.mkdirSync("./log");
} else {
process.on('exit', () => {
fs.writeFileSync("./log/log.txt", Time() + text, 'utf-8');
});
};
},
};
function Time() {
d = new Date();
datetext = d.toTimeString();
datetext = datetext.split(' ')[0];
return "[" + datetext + "] ";
};
module.exports = log;