@@ -5,9 +5,17 @@ const os = require('os')
5
5
const test = require ( 'tap' ) . test
6
6
const pino = require ( 'pino' )
7
7
const dateformat = require ( 'dateformat' )
8
+ const path = require ( 'path' )
9
+ const rimraf = require ( 'rimraf' )
10
+ const { join } = require ( 'path' )
11
+ const fs = require ( 'fs' )
8
12
const pinoPretty = require ( '..' )
13
+ const SonicBoom = require ( 'sonic-boom' )
9
14
const _prettyFactory = pinoPretty . prettyFactory
10
15
16
+ // Disable pino warnings
17
+ process . removeAllListeners ( 'warning' )
18
+
11
19
function prettyFactory ( opts ) {
12
20
if ( ! opts ) {
13
21
opts = { colorize : false }
@@ -748,5 +756,56 @@ test('basic prettifier tests', (t) => {
748
756
t . doesNotThrow ( pinoPretty )
749
757
} )
750
758
759
+ t . test ( 'stream usage' , async ( t ) => {
760
+ t . plan ( 1 )
761
+ const tmpDir = path . join ( __dirname , '.tmp_' + Date . now ( ) )
762
+ t . teardown ( ( ) => rimraf ( tmpDir , noop ) )
763
+
764
+ const destination = join ( tmpDir , 'output' )
765
+
766
+ const pretty = pinoPretty ( {
767
+ singleLine : true ,
768
+ colorize : false ,
769
+ mkdir : true ,
770
+ append : false ,
771
+ destination : new SonicBoom ( { dest : destination , async : false , mkdir : true , append : true } ) ,
772
+ customPrettifiers : {
773
+ upper : val => val . toUpperCase ( ) ,
774
+ undef : ( ) => undefined
775
+ }
776
+ } )
777
+ const log = pino ( pretty )
778
+ log . info ( { msg : 'message' , extra : { foo : 'bar' , number : 42 } , upper : 'foobar' , undef : 'this will not show up' } )
779
+
780
+ await watchFileCreated ( destination )
781
+
782
+ const formatted = fs . readFileSync ( destination , 'utf8' )
783
+
784
+ t . equal ( formatted , `[${ epoch } ] INFO (${ pid } on ${ hostname } ): message {"extra":{"foo":"bar","number":42},"upper":"FOOBAR"}\n` )
785
+ } )
786
+
751
787
t . end ( )
752
788
} )
789
+
790
+ function watchFileCreated ( filename ) {
791
+ return new Promise ( ( resolve , reject ) => {
792
+ const TIMEOUT = 2000
793
+ const INTERVAL = 100
794
+ const threshold = TIMEOUT / INTERVAL
795
+ let counter = 0
796
+ const interval = setInterval ( ( ) => {
797
+ // On some CI runs file is created but not filled
798
+ if ( fs . existsSync ( filename ) && fs . statSync ( filename ) . size !== 0 ) {
799
+ clearInterval ( interval )
800
+ resolve ( )
801
+ } else if ( counter <= threshold ) {
802
+ counter ++
803
+ } else {
804
+ clearInterval ( interval )
805
+ reject ( new Error ( `${ filename } was not created.` ) )
806
+ }
807
+ } , INTERVAL )
808
+ } )
809
+ }
810
+
811
+ function noop ( ) { }
0 commit comments