File tree 4 files changed +36
-10
lines changed
4 files changed +36
-10
lines changed Original file line number Diff line number Diff line change @@ -56,6 +56,7 @@ This package supports several command line arguments to customize the output. A
56
56
| ` --date ` , ` -d ` | ` boolean ` | ` false ` | Show a date in the log output. |
57
57
| ` --time ` , ` -t ` | ` boolean ` | ` true ` | Show a time in the log output. Use ` --no-time ` to disable. |
58
58
| ` --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. |
59
60
| ` --stacktrace ` , ` -s ` | ` boolean ` | ` true ` | Show a stack trace for errors that occur. Use ` --no-stacktrace ` to disable. |
60
61
| ` --colorize ` , ` -c ` | ` boolean ` | Depends on terminal | Colorize the console output. Use ` --no-colorize ` to disable. |
61
62
| ` --messageKey ` | ` string ` | ` msg ` | The JSON key to read the message from. |
Original file line number Diff line number Diff line change 26
26
describe : "Show PID" ,
27
27
type : "boolean"
28
28
} )
29
+ . option ( "hostname" , {
30
+ alias : "h" ,
31
+ default : false ,
32
+ describe : "Show hostname" ,
33
+ type : "boolean"
34
+ } )
29
35
. option ( "stacktrace" , {
30
36
alias : "s" ,
31
37
default : true ,
Original file line number Diff line number Diff line change @@ -15,7 +15,8 @@ const argvMock = {
15
15
stacktrace : true ,
16
16
colorize : true ,
17
17
crlf : false ,
18
- pid : false
18
+ pid : false ,
19
+ hostname : false
19
20
} ;
20
21
21
22
// Prepare a default Pino output
@@ -26,7 +27,8 @@ const pinoMock = (data = {}, messageEmpty = false) => {
26
27
time : data . time || ( new Date ) . getTime ( ) ,
27
28
msg : messageEmpty ? undefined : data . message || "custom message" ,
28
29
stack : data . stack || undefined ,
29
- pid : process . pid
30
+ pid : process . pid ,
31
+ hostname : "hostname"
30
32
} ) ;
31
33
}
32
34
@@ -151,6 +153,18 @@ describe ("Command line arguments", _ => {
151
153
done ( ) ;
152
154
} )
153
155
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
+
154
168
it ( "should accept a --stacktrace option" , done => {
155
169
try {
156
170
undefinedVar1 + undefinedVar2 ;
Original file line number Diff line number Diff line change @@ -44,18 +44,23 @@ const formatExtras = ({
44
44
data,
45
45
options : {
46
46
colorize : shouldColorize ,
47
- pid : showPid
47
+ pid : showPid ,
48
+ hostname : showHostname
48
49
}
49
50
} ) => {
50
- // Grab PID
51
- const { pid } = data ;
51
+ // Grab PID and hostname
52
+ const { pid, hostname } = data ;
52
53
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 } ]` : "" ;
55
57
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 ) ) ;
59
64
}
60
65
61
66
/**
You can’t perform that action at this time.
0 commit comments