Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / lib / appenders / loggly.js
1 'use strict';
2 var layouts = require('../layouts')
3 , loggly = require('loggly')
4 , os = require('os')
5 , passThrough = layouts.messagePassThroughLayout;
6
7
8 function isAnyObject(value) {
9         return value !== null && (typeof value === 'object' || typeof value === 'function');
10 }
11
12 function numKeys(o) {
13   var res = 0;
14   for (var k in o) {
15     if (o.hasOwnProperty(k)) res++;
16   }
17   return res;
18 }
19
20 /**
21  * @param msg - array of args for logging.
22  * @returns { deTaggedMsg: [], additionalTags: [] }
23  */
24 function processTags(msgListArgs) {
25   var msgList = (msgListArgs.length === 1 ? [msgListArgs[0]] : Array.apply(null, msgListArgs));
26
27   return msgList.reduce(function (accum, element, currentIndex, array) {
28     if (isAnyObject(element) && Array.isArray(element.tags) && numKeys(element) == 1) {
29       accum.additionalTags = accum.additionalTags.concat(element.tags);
30     } else {
31       accum.deTaggedData.push(element);
32     }
33     return accum;
34   }, { deTaggedData: [], additionalTags: [] });
35 }
36
37 /**
38  * Loggly Appender. Sends logging events to Loggly using node-loggly, optionally adding tags.
39  *
40  * This appender will scan the msg from the logging event, and pull out any argument of the
41  * shape `{ tags: [] }` so that it's possibleto add tags in a normal logging call.
42  *
43  * For example:
44  *
45  * logger.info({ tags: ['my-tag-1', 'my-tag-2'] }, 'Some message', someObj, ...)
46  *
47  * And then this appender will remove the tags param and append it to the config.tags.
48  *
49  * @param config object with loggly configuration data
50  * {
51  *   token: 'your-really-long-input-token',
52  *   subdomain: 'your-subdomain',
53  *   tags: ['loggly-tag1', 'loggly-tag2', .., 'loggly-tagn']
54  * }
55  * @param layout a function that takes a logevent and returns a string (defaults to objectLayout).
56  */
57 function logglyAppender(config, layout) {
58         var client = loggly.createClient(config);
59   if(!layout) layout = passThrough;
60
61   return function(loggingEvent) {
62     var result = processTags(loggingEvent.data);
63     var deTaggedData = result.deTaggedData;
64     var additionalTags = result.additionalTags;
65
66     // Replace the data property with the deTaggedData
67     loggingEvent.data = deTaggedData;
68
69     var msg = layout(loggingEvent);
70
71                 client.log({
72                         msg: msg,
73                         level: loggingEvent.level.levelStr,
74                         category: loggingEvent.categoryName,
75                         hostname: os.hostname().toString(),
76                 }, additionalTags);
77   };
78 }
79
80 function configure(config) {
81         var layout;
82         if (config.layout) {
83                 layout = layouts.layout(config.layout.type, config.layout);
84         }
85         return logglyAppender(config, layout);
86 }
87
88 exports.name      = 'loggly';
89 exports.appender  = logglyAppender;
90 exports.configure = configure;