Last active
August 29, 2015 14:06
-
-
Save merqlove/0d0a177696e78b52cfe0 to your computer and use it in GitHub Desktop.
AngularJS / NodeWebkit logger service.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
angular.module('vc.messages') | |
.factory "logger", ['$log', '$location', '$timeout', 'NwService', 'stackTrace', ($log, $location, $timeout, NwService, stackTrace) -> | |
log4js = require('log4js') | |
airbrake = require('airbrake').createClient('SOMEKEY') | |
airbrake.protocol = 'http' | |
airbrake.serviceHost = 'api.airbrake.io' | |
airbrake.env = 'production' if NwService.isProduction() | |
airbrake.on 'vars', (type, vars) -> | |
if type is 'cgi-data' | |
delete vars.AWS_ACCESS_KEY_ID | |
delete vars.AWS_SECRET_ACCESS_KEY | |
delete vars.DIGITAL_OCEAN_API_KEY | |
delete vars.DIGITAL_OCEAN_CLIENT_ID | |
async = require('async') | |
logger = {} | |
levels = ['trace','debug','info','warn','error','fatal'] | |
consoleLevel = if NwService.isProduction() | |
"OFF" | |
else | |
"TRACE" | |
log4js.configure { | |
appenders: [ | |
{ | |
type: 'console' | |
layout: { | |
type: 'pattern' | |
pattern: "%m" | |
} | |
category: 'console' | |
}, | |
{ | |
type: 'file' | |
absolute: true | |
filename: NwService.dataPath() + '/app.log' | |
maxLogSize: 1024*1024 | |
numBackups: 3 | |
layout: { | |
type: 'pattern' | |
pattern: "[%d %r] [%p] - %m%n" | |
} | |
category: 'file' | |
}, | |
] | |
levels: { | |
"console": consoleLevel, | |
"file": "ERROR" | |
} | |
} | |
consoleLogger = log4js.getLogger('console') | |
fileLogger = log4js.getLogger('file') | |
levels.forEach (levelString) -> | |
level = log4js.levels.toLevel(levelString) | |
consoleLogger[levelString] = (args...) -> | |
return unless consoleLogger.isLevelEnabled(level) | |
switch levelString | |
when 'info', 'warn' | |
$log[levelString].apply($log, args) | |
else | |
args.unshift(level) | |
consoleLogger.log.apply(consoleLogger, args) | |
logger[levelString] = (args...) -> | |
$timeout( () -> | |
switch levelString | |
when 'error', 'fatal' | |
if _.isString(args[0].reason) | |
args[0].reason = args[0].reason.split(/\n/g) | |
else | |
args[0].reason ||= stackTrace.getStack(4) | |
consoleLogger[levelString].apply(consoleLogger, args) | |
args[0].url ||= $location.path() | |
when 'trace' | |
if _.isObject(args[0]) | |
args[0].stack ||= stackTrace.getStack(4) | |
consoleLogger[levelString].apply(consoleLogger, args) | |
else | |
consoleLogger[levelString].apply(consoleLogger, args) | |
async.forEach( | |
[args], | |
(args, cb) -> | |
fileLogger[levelString].apply(fileLogger, args) | |
if _.include(['error', 'fatal'], levelString) && airbrake.env is 'production' | |
args[0].stack ||= args[0].reason.join("\n") | |
delete args[0].reason | |
airbrake.notify args[0], (err, url) -> | |
throw err if err? | |
cb() | |
(err) -> | |
console.log(err) if err? | |
) | |
0, false) | |
return logger | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment