Skip to content

Commit b37874f

Browse files
committed
Add option to show hostname
1 parent 5bffd8c commit b37874f

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ This package supports several command line arguments to customize the output. A
5656
| `--date`, `-d` | `boolean` | `false` | Show a date in the log output. |
5757
| `--time`, `-t` | `boolean` | `true` | Show a time in the log output. Use `--no-time` to disable. |
5858
| `--pid`, `-p` | `boolean` | `false` | Show the process ID (PID) in the log output. |
59+
| `--hostname`, `-h` | `boolean` | `false` | Show the hostname in the log output. |
5960
| `--stacktrace`, `-s` | `boolean` | `true` | Show a stack trace for errors that occur. Use `--no-stacktrace` to disable. |
6061
| `--colorize`, `-c` | `boolean` | Depends on terminal | Colorize the console output. Use `--no-colorize` to disable. |
6162
| `--messageKey` | `string` | `msg` | The JSON key to read the message from. |

lib/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ yargs
2626
describe: "Show PID",
2727
type: "boolean"
2828
})
29+
.option("hostname", {
30+
alias: "h",
31+
default: false,
32+
describe: "Show hostname",
33+
type: "boolean"
34+
})
2935
.option("stacktrace", {
3036
alias: "s",
3137
default: true,

lib/test.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const argvMock = {
1515
stacktrace: true,
1616
colorize: true,
1717
crlf: false,
18-
pid: false
18+
pid: false,
19+
hostname: false
1920
};
2021

2122
// Prepare a default Pino output
@@ -26,7 +27,8 @@ const pinoMock = (data = {}, messageEmpty = false) => {
2627
time: data.time || (new Date).getTime (),
2728
msg: messageEmpty ? undefined : data.message || "custom message",
2829
stack: data.stack || undefined,
29-
pid: process.pid
30+
pid: process.pid,
31+
hostname: "hostname"
3032
});
3133
}
3234

@@ -151,6 +153,18 @@ describe ("Command line arguments", _ => {
151153
done ();
152154
})
153155

156+
it ("should accept a --hostname option", done => {
157+
const mock = pinoMock ();
158+
159+
const withHostname = parseLine (mock, { ...argvMock, hostname: true });
160+
const withoutHostname = parseLine (mock, { ...argvMock, hostname: false });
161+
162+
assert.equal (withoutHostname.includes (`[hostname]`), false);
163+
assert.equal (withHostname.includes (`[hostname]`), true);
164+
165+
done ();
166+
})
167+
154168
it ("should accept a --stacktrace option", done => {
155169
try {
156170
undefinedVar1 + undefinedVar2;

lib/util.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,23 @@ const formatExtras = ({
4444
data,
4545
options: {
4646
colorize: shouldColorize,
47-
pid: showPid
47+
pid: showPid,
48+
hostname: showHostname
4849
}
4950
}) => {
50-
// Grab PID
51-
const { pid } = data;
51+
// Grab PID and hostname
52+
const { pid, hostname } = data;
5253

53-
// Format the date using date-fns
54-
let formattedPid = showPid ? `[${pid}]` : "";
54+
// Format extras
55+
const formattedPid = (showPid && pid) ? `[${pid}]` : "";
56+
const formattedHostname = (showHostname && hostname) ? `[${hostname}]` : "";
5557

56-
// Return colored string
57-
if (!shouldColorize) return formattedPid;
58-
return chalk.dim (chalk.gray(formattedPid));
58+
// Concatenate all extras
59+
const extras = formattedPid + formattedHostname;
60+
61+
// Return colored strings
62+
if (!shouldColorize) return extras;
63+
return chalk.dim (chalk.gray(extras));
5964
}
6065

6166
/**

0 commit comments

Comments
 (0)