|
@@ -0,0 +1,102 @@
|
|
|
+let logger = null
|
|
|
+
|
|
|
+class Logger {
|
|
|
+ static levels = ['debug', 'info', 'warn', 'error']
|
|
|
+
|
|
|
+ constructor(level) {
|
|
|
+ this.level = level
|
|
|
+ this.isBrowser = typeof process === 'undefined' ||
|
|
|
+ process.type === 'renderer' ||
|
|
|
+ process.browser === true ||
|
|
|
+ process.__nwjs
|
|
|
+ if (!this.isBrowser) {
|
|
|
+ this.colors = {
|
|
|
+ start: '\x1b[2m',
|
|
|
+ warn: '\x1b[35m',
|
|
|
+ info: '\x1b[33m',
|
|
|
+ debug: '\x1b[36m',
|
|
|
+ error: '\x1b[31m',
|
|
|
+ end: '\x1b[0m',
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ this.colors = {
|
|
|
+ start: '%c',
|
|
|
+ warn: 'color : #ff00ff',
|
|
|
+ info: 'color : #ffff00',
|
|
|
+ debug: 'color : #00ffff',
|
|
|
+ error: 'color : #ff0000',
|
|
|
+ end: '',
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.messageFormat = '[%t] [%l] - [%m]'
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param level {string}
|
|
|
+ * @returns {boolean}
|
|
|
+ */
|
|
|
+ canSend(level) {
|
|
|
+ return (Logger.levels.indexOf(this.level) <= Logger.levels.indexOf(level))
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param message {string}
|
|
|
+ */
|
|
|
+ warn(message) {
|
|
|
+ this._log('warn', message, this.colors.warn)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param message {string}
|
|
|
+ */
|
|
|
+ info(message) {
|
|
|
+ this._log('info', message, this.colors.info)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param message {string}
|
|
|
+ */
|
|
|
+ debug(message) {
|
|
|
+ this._log('debug', message, this.colors.debug)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param message {string}
|
|
|
+ */
|
|
|
+ error(message) {
|
|
|
+ this._log('error', message, this.colors.error)
|
|
|
+ }
|
|
|
+
|
|
|
+ format(message, level) {
|
|
|
+ return this.messageFormat.replace('%t', new Date().toISOString())
|
|
|
+ .replace('%l', level.toUpperCase())
|
|
|
+ .replace('%m', message)
|
|
|
+ }
|
|
|
+
|
|
|
+ static getLogger() {
|
|
|
+ if (!logger) {
|
|
|
+ logger = new Logger('debug')
|
|
|
+ }
|
|
|
+ return logger
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param level {string}
|
|
|
+ * @param message {string}
|
|
|
+ * @param color {string}
|
|
|
+ */
|
|
|
+ _log(level, message, color) {
|
|
|
+ if (this.canSend(level)) {
|
|
|
+ if (!this.isBrowser) {
|
|
|
+ console.log(color + this.format(message, level) + this.colors.end)
|
|
|
+ } else {
|
|
|
+ console.log(this.colors.start + this.format(message, level), color)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ console.log('can\'t send')
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = Logger
|