UNPKG

2.89 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');
33
34test('writes pid and host to backends', function (assert) {
35 var loc = path.join(os.tmpDir(), uuid());
36 var pid = process.pid;
37 var host = os.hostname();
38
39 var logger = Logger({
40 meta: {
41 team: 'rt',
42 project: 'foobar',
43 hostname: os.hostname(),
44 pid: process.pid
45 },
46 backends: {
47 disk: DiskBackend({
48 folder: loc
49 }),
50 console: ConsoleBackend()
51 }
52 });
53
54 assert.ok(captureStdio('_pid=' + pid, function () {
55 logger.info('hello', { foo: 'bar' }, onlog);
56 }));
57
58 function onlog(err) {
59 assert.ifError(err);
60
61 assert.ok(captureStdio('_hostname=' + host, function () {
62 logger.info('hello', { foo: 'bar' }, onlog2);
63 }));
64 }
65
66 function onlog2(err) {
67 assert.ifError(err);
68
69 var fileUri = path.join(loc, 'rt-foobar.log-' +
70 dateFormat('yyyyMMdd'));
71
72 fs.readFile(fileUri, function (err, buf) {
73 assert.ifError(err);
74
75 buf = String(buf);
76
77 assert.ok(buf.indexOf('hello') !== -1);
78 assert.ok(buf.indexOf('foo=bar') !== -1);
79 assert.ok(buf.indexOf('_pid=' + pid) !== -1);
80 assert.ok(buf.indexOf('_hostname=' + host) !== -1);
81
82 rimraf(loc, assert.end);
83 });
84 }
85});