UNPKG

3.42 kBJavaScriptView Raw
1// Copyright (c) 2015 Uber Technologies, Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21var test = require('tape');
22var path = require('path');
23var os = require('os');
24var uuid = require('uuid');
25var fs = require('fs');
26var dateFormat = require('date-format');
27var rimraf = require('rimraf');
28
29var captureStdio = require('./lib/capture-stdio.js');
30var Logger = require('../logger.js');
31var DiskBackend = require('../backends/disk.js');
32var ConsoleBackend = require('../backends/console.js');
33var Entry = require('../entry.js');
34
35function AddFoo(baseMeta) {
36 return addFoo;
37
38 function addFoo(entry) {
39 var meta = entry.meta || {};
40
41 if (!meta._foo && baseMeta.foo) {
42 meta._foo = baseMeta.foo;
43 }
44
45 return new Entry(entry.level, entry.message, entry.meta, entry.path);
46 }
47}
48
49test('writes extended meta to backends', function (assert) {
50 var loc = path.join(os.tmpDir(), uuid());
51 var pid = process.pid;
52 var foo = 'foop';
53 var host = os.hostname();
54
55 var logger = Logger({
56 meta: {
57 team: 'rt',
58 project: 'foobar',
59 hostname: os.hostname(),
60 pid: process.pid,
61 foo: foo
62 },
63 basemetaTransforms: [AddFoo],
64 backends: {
65 disk: DiskBackend({
66 folder: loc
67 }),
68 console: ConsoleBackend()
69 }
70 });
71
72 assert.ok(captureStdio('_foo=' + foo, function () {
73 logger.info('hello', { foo: 'bar' }, onlog);
74 }));
75
76 function onlog(err) {
77 assert.ifError(err);
78
79 assert.ok(captureStdio('_hostname=' + host, function () {
80 logger.meta.foo = 'bar';
81 foo = 'bar';
82 logger.info('hello', { foo: 'bar' }, onlog2);
83 }));
84 }
85
86 function onlog2(err) {
87 assert.ifError(err);
88
89 var fileUri = path.join(loc, 'rt-foobar.log-' +
90 dateFormat('yyyyMMdd'));
91
92 fs.readFile(fileUri, function (err, buf) {
93 assert.ifError(err);
94
95 buf = String(buf);
96
97 assert.ok(buf.indexOf('hello') !== -1);
98 assert.ok(buf.indexOf('foo=bar') !== -1);
99 assert.ok(buf.indexOf('_pid=' + pid) !== -1);
100 assert.ok(buf.indexOf('_foo=' + foo) !== -1);
101 assert.ok(buf.indexOf('_hostname=' + host) !== -1);
102
103 rimraf(loc, assert.end);
104 });
105 }
106});