smartlog-destination-local/ts/beautylog.log.helpers.ts

113 lines
4.3 KiB
TypeScript
Raw Normal View History

2016-10-16 00:26:43 +00:00
import * as plugins from './beautylog.plugins'
import { remoteLog } from './beautylog.remote'
2016-07-23 23:17:38 +00:00
/**
*
* @param logText
* @param logType
* @returns {boolean}
*/
export let internalLog = function (logType: string = 'normal', logText: string = 'empty log') {
2016-10-16 00:26:43 +00:00
switch (plugins.smartenv.getEnv().runtimeEnv) {
case 'node':
logNode(logType, logText)
remoteLog(logType, logText)
break
case 'browser':
logBrowser(logText, logType)
break
2016-07-23 23:17:38 +00:00
default:
2016-10-16 00:26:43 +00:00
console.log('something is strange about the platform in which you try to use beautylog')
break
}
}
2017-01-21 17:40:40 +00:00
let coloredString = plugins.beautycolor.coloredString
2016-05-02 00:23:40 +00:00
2016-06-16 00:17:28 +00:00
let localBl = {
2017-01-21 17:40:40 +00:00
dirPrefix: coloredString(' DIR ', 'white', 'blue') + ' ',
2017-01-21 22:12:39 +00:00
errorPrefix: coloredString(' ', 'red','red') + coloredString(' ERROR! ', 'red', 'black') + ' ',
infoPrefix: coloredString(' ', 'blue', 'blue') + coloredString(' INFO: ', 'blue', 'black') + ' ',
logPrefix: coloredString(' ', 'white', 'cyan') + coloredString(' LOG: ', 'cyan', 'black') + ' ',
notePrefix: coloredString(' ', 'pink', 'pink') + coloredString( ' NOTE -> ', 'pink', 'black') + ' ',
2017-01-21 17:40:40 +00:00
okPrefix: coloredString(' ', 'green', 'green') + coloredString(' OK! ', 'green', 'black') + ' ',
2017-01-21 22:12:39 +00:00
successPrefix: coloredString(' ', 'green', 'green') + coloredString(' SUCCESS! ', 'green', 'black') + ' ',
warnPrefix: coloredString(' ', 'orange', 'orange') + coloredString(' WARN: -> ', 'orange', 'black') + ' '
2016-10-16 00:26:43 +00:00
}
2016-05-02 00:23:40 +00:00
2016-10-16 00:26:43 +00:00
export let logNode = function (logType: string, logText: string) {
2016-05-02 00:23:40 +00:00
try {
switch (logType) {
case 'dir':
2017-01-21 17:40:40 +00:00
logText = localBl.dirPrefix + coloredString(logText, 'blue')
2016-10-16 00:26:43 +00:00
break
2016-05-02 00:23:40 +00:00
case 'error':
2017-01-21 17:40:40 +00:00
logText = localBl.errorPrefix + coloredString(logText, 'red')
2016-10-16 00:26:43 +00:00
break
2016-05-02 00:23:40 +00:00
case 'info':
2017-01-21 20:11:41 +00:00
logText = localBl.infoPrefix + coloredString(logText, 'blue')
2016-10-16 00:26:43 +00:00
break
2016-05-02 00:23:40 +00:00
case 'normal':
2017-01-21 17:40:40 +00:00
logText = localBl.logPrefix + coloredString(logText, 'cyan')
2016-10-16 00:26:43 +00:00
break
2017-01-21 22:12:39 +00:00
case 'note':
logText = localBl.notePrefix + coloredString(logText, 'pink')
break
2016-05-02 00:23:40 +00:00
case 'ok':
2017-01-21 18:52:31 +00:00
logText = localBl.okPrefix + coloredString(logText, 'green')
2016-10-16 00:26:43 +00:00
break
2016-05-02 00:23:40 +00:00
case 'success':
2017-01-21 17:40:40 +00:00
logText = localBl.successPrefix + coloredString(logText, 'green')
2016-10-16 00:26:43 +00:00
break
2016-05-02 00:23:40 +00:00
case 'warn':
2017-01-21 17:40:40 +00:00
logText = localBl.warnPrefix + coloredString(logText, 'orange')
2016-10-16 00:26:43 +00:00
break
2016-05-02 00:23:40 +00:00
case 'log':
2017-01-21 17:40:40 +00:00
logText = localBl.logPrefix + coloredString(logText, 'cyan')
2016-10-16 00:26:43 +00:00
break
2016-05-02 00:23:40 +00:00
default:
2017-01-21 17:40:40 +00:00
coloredString(logText, 'blue')
2016-10-16 00:26:43 +00:00
console.log(('unknown logType for "' + logText + '"'))
break
2016-05-02 00:23:40 +00:00
}
2017-01-21 22:12:39 +00:00
console.log(logText)
2016-10-16 00:26:43 +00:00
return true
} catch (error) {
console.log(localBl.errorPrefix
+ 'You seem to have tried logging something strange' + error)
return false
2016-05-02 00:23:40 +00:00
}
}
2016-07-23 23:17:38 +00:00
let logBrowser = function (logText, logType) {
2016-05-02 00:23:40 +00:00
switch (logType) {
2016-07-23 23:17:38 +00:00
case 'dir':
2017-01-21 17:40:40 +00:00
logText = localBl.dirPrefix + coloredString(logText, 'blue')
2016-10-16 00:26:43 +00:00
break
2016-07-23 23:17:38 +00:00
case 'error':
2016-10-16 00:26:43 +00:00
logText = localBl.errorPrefix + logText.red.bold
break
2016-07-23 23:17:38 +00:00
case 'info':
2016-10-16 00:26:43 +00:00
console.log('%c Info: %c ' + logText, 'background:#EC407A;color:#ffffff;', 'color:#EC407A;')
break
2016-07-23 23:17:38 +00:00
case 'normal':
2016-10-16 00:26:43 +00:00
logText = localBl.logPrefix + logText.cyan.bold
break
2016-07-23 23:17:38 +00:00
case 'ok':
2016-10-16 00:26:43 +00:00
console.log('%c OK: %c ' + logText, 'background:#000000;color:#8BC34A;', 'color:#000000;')
break
2016-07-23 23:17:38 +00:00
case 'success':
2016-10-16 00:26:43 +00:00
console.log('%c Success: %c ' + logText, 'background:#8BC34A;color:#ffffff;', 'color:#8BC34A;')
break
2016-07-23 23:17:38 +00:00
case 'warn':
2016-10-16 00:26:43 +00:00
console.log('%c Warn: %c ' + logText, 'background:#000000;color:#FB8C00;', 'color:#000000;')
break
2016-07-23 23:17:38 +00:00
case 'log':
2016-10-16 00:26:43 +00:00
console.log('%c Log: %c ' + logText, 'background:#42A5F5;color:#ffffff', 'color:#42A5F5;')
break
2016-07-23 23:17:38 +00:00
default:
2016-10-16 00:26:43 +00:00
console.log('unknown logType for "' + logText + '"')
break
2016-07-23 23:17:38 +00:00
}
2017-01-21 00:05:28 +00:00
}