1 | _ = require 'underscore'
|
2 | adaptersFactory = require('./logasm/adapters')
|
3 | preprocessorsFactory = require('./logasm/preprocessors')
|
4 |
|
5 | class Logasm
|
6 | @build: (serviceName, loggersConfig, preprocessorsConfig = {}) ->
|
7 | loggersConfig = {stdout: {}} if loggersConfig is undefined
|
8 | adapters = for type, args of loggersConfig
|
9 | adaptersFactory.getAdapter(type, serviceName, args)
|
10 | preprocessors = for type, args of preprocessorsConfig
|
11 | preprocessorsFactory.getPreprocessor(type, args)
|
12 |
|
13 | new Logasm(adapters, preprocessors)
|
14 |
|
15 | constructor:(@adapters, @preprocessors) ->
|
16 |
|
17 | silly: =>
|
18 | @log 'silly', arguments
|
19 |
|
20 | debug: =>
|
21 | @log 'debug', arguments
|
22 |
|
23 | verbose: =>
|
24 | @log 'verbose', arguments
|
25 |
|
26 | info: =>
|
27 | @log 'info', arguments
|
28 |
|
29 | warn: =>
|
30 | @log 'warn', arguments
|
31 |
|
32 | error: =>
|
33 | @log 'error', arguments
|
34 |
|
35 | log: (level, args) =>
|
36 | data = @parseLogData.apply(@, args)
|
37 | processedData = @preprocess(data)
|
38 | for adapter in @adapters
|
39 | adapter.log level, processedData
|
40 |
|
41 | preprocess: (data) =>
|
42 | _.reduce(@preprocessors, (processedData, preprocessor) =>
|
43 | preprocessor.process(processedData)
|
44 | , data)
|
45 |
|
46 | parseLogData: (message, metadata) ->
|
47 | if @isHash(message)
|
48 | message
|
49 | else
|
50 | data = {}
|
51 |
|
52 | if metadata instanceof Error
|
53 | data['error'] = metadata.toString()
|
54 | data['error.stack_trace'] = metadata.stack if metadata.stack
|
55 | else
|
56 | _.extend(data, metadata)
|
57 |
|
58 | if message instanceof Error
|
59 | data['error'] = message.toString()
|
60 | data['error.stack_trace'] = message.stack if message.stack
|
61 |
|
62 | data['message'] = message
|
63 | data
|
64 |
|
65 | isHash: (obj) =>
|
66 | return false unless obj instanceof Object
|
67 | if Object.keys(obj).length then true else false
|
68 |
|
69 | module.exports = Logasm
|