@@ -14,7 +14,6 @@ import { v4 as uuidv4 } from 'uuid';
1414import { default as parseFunction } from 'parse-function'
1515import * as fs from 'fs'
1616import datauri from 'datauri'
17- import pino from 'pino'
1817import isUrl from 'is-url-superb'
1918import { readJsonSync } from 'fs-extra'
2019import treekill from 'tree-kill' ;
@@ -39,28 +38,11 @@ import { base64MimeType, getDUrl, isBase64, isDataURL } from '../utils/tools';
3938import { Call } from './model/call' ;
4039import { Button , Section } from './model/button' ;
4140import { JsonObject } from 'type-fest' ;
41+ import { log } from '../utils/logging' ;
4242
43- /** @ignore */
44- const pkg = readJsonSync ( path . join ( __dirname , '../../package.json' ) ) ,
45- createLogger = ( sessionId : string , sessionInfo : SessionInfo , config : ConfigObject ) => {
46- const p = path . join ( path . resolve ( process . cwd ( ) ) , `/logs/${ sessionId || 'session' } /${ sessionInfo . START_TS } .log` )
47- if ( ! fs . existsSync ( p ) ) {
48- fs . mkdirSync ( path . join ( path . resolve ( process . cwd ( ) ) , `/logs/${ sessionId || 'session' } ` ) , {
49- recursive :true
50- } )
51- }
52- const logger = pino ( {
53- redact : [ 'file' , 'base64' , 'image' , 'webpBase64' , 'base64' , 'durl' , 'thumbnail' ] ,
54- } , pino . destination ( p ) )
55-
56- logger . child ( {
57- "STAGE" : "LAUNCH" ,
58- sessionInfo,
59- config
60- } ) . info ( "" )
6143
62- return logger
63- }
44+ /** @ignore */
45+ const pkg = readJsonSync ( path . join ( __dirname , '../../package.json' ) ) ;
6446
6547export enum namespace {
6648 Chat = 'Chat' ,
@@ -289,7 +271,6 @@ export class Client {
289271 private _page : Page ;
290272 private _currentlyBeingKilled = false ;
291273 private _refreshing = false
292- private _l : any ;
293274 private _prio : number = Number . MAX_SAFE_INTEGER ;
294275 private _pageListeners : {
295276 event : keyof PageEventObject ,
@@ -337,10 +318,9 @@ export class Client {
337318 await this . registerAllSimpleListenersOnEv ( ) ;
338319 }
339320 this . _sessionInfo . PHONE_VERSION = ( await this . getMe ( ) ) ?. phone ?. wa_version
340- this . logger ( ) . child ( {
321+ log . info ( 'LOADED' , {
341322 PHONE_VERSION : this . _sessionInfo . PHONE_VERSION
342- } ) . info ( )
343-
323+ } )
344324 if ( this . _createConfig ?. autoEmoji === undefined || this . _createConfig ?. autoEmoji ) {
345325 const ident = typeof this . _createConfig ?. autoEmoji === "string" ? this . _createConfig ?. autoEmoji : ":"
346326 this . onMessage ( async message => {
@@ -356,6 +336,7 @@ export class Client {
356336 if ( this . _createConfig ?. deleteSessionDataOnLogout ) deleteSessionData ( this . _createConfig )
357337 if ( this . _createConfig ?. killClientOnLogout ) {
358338 console . log ( "Session logged out. Killing client" )
339+ log . warn ( "Session logged out. Killing client" )
359340 this . kill ( ) ;
360341 }
361342 } )
@@ -378,6 +359,7 @@ export class Client {
378359 this . _page . on ( 'close' , ( ) => {
379360 if ( ! this . _refreshing ) {
380361 console . log ( "Browser page has closed. Killing client" )
362+ log . warn ( "Browser page has closed. Killing client" )
381363 this . kill ( ) ;
382364 if ( this . _createConfig ?. killProcessOnBrowserClose ) process . exit ( ) ;
383365 }
@@ -407,8 +389,7 @@ export class Client {
407389 * Grab the logger for this session/process
408390 */
409391 public logger ( ) : any {
410- if ( ! this . _l ) this . _l = createLogger ( this . getSessionId ( ) , this . getSessionInfo ( ) , this . getConfig ( ) ) ;
411- return this . _l ;
392+ return log ;
412393 }
413394
414395 /**
@@ -520,7 +501,8 @@ export class Client {
520501
521502
522503 private async pup ( pageFunction :EvaluateFn < any > , ...args ) {
523- const { safeMode, callTimeout, idChecking, logFile} = this . _createConfig ;
504+ const { safeMode, callTimeout, idChecking, logFile, logging} = this . _createConfig ;
505+ let _t : number ;
524506 if ( safeMode ) {
525507 if ( ! this . _page || this . _page . isClosed ( ) ) throw new CustomError ( ERROR_NAME . PAGE_CLOSED , 'page closed' ) ;
526508 const state = await this . forceUpdateConnectionState ( ) ;
@@ -537,15 +519,19 @@ export class Client {
537519 }
538520 } )
539521 }
540- if ( logFile ) {
522+ if ( logging ) {
541523 const wapis = ( pageFunction ?. toString ( ) ?. match ( / W A P I \. ( \w * ) \( / g) || [ ] ) ?. map ( s => s . replace ( / W A P I | \. | \( / g, '' ) ) ;
542- this . logger ( ) . child ( {
543- _method : wapis ?. length === 1 ? wapis [ 0 ] : wapis ,
544- ...args [ 0 ]
545- } ) . info ( )
524+ _t = Date . now ( )
525+ log . info ( `Request ${ _t } ` , {
526+ _method : wapis ?. length === 1 ? wapis [ 0 ] : wapis ,
527+ ...args [ 0 ]
528+ } )
546529 }
547530 if ( callTimeout ) return await Promise . race ( [ this . _page . evaluate ( pageFunction , ...args ) , new Promise ( ( resolve , reject ) => setTimeout ( reject , this . _createConfig ?. callTimeout , new PageEvaluationTimeout ( ) ) ) ] )
548531 const res = await this . _page . evaluate ( pageFunction , ...args ) ;
532+ if ( _t && logging ) {
533+ log . info ( `Response ${ _t } : ${ Date . now ( ) - _t } ms` , { res} )
534+ }
549535 if ( this . _createConfig . onError && typeof res == "string" && ( res . startsWith ( "Error" ) || res . startsWith ( "ERROR" ) ) ) {
550536 const e = this . _createConfig . onError ;
551537 /**
@@ -617,7 +603,6 @@ export class Client {
617603 return window [ funcName ] ? WAPI [ `${ funcName } ` ] ( obj => window [ funcName ] ( obj ) ) : false
618604 } , { funcName} ) ;
619605 if ( this . _listeners [ funcName ] && ! this . _refreshing ) {
620- // console.log('listener already set');
621606 return true
622607 }
623608 this . _listeners [ funcName ] = fn ;
@@ -654,6 +639,7 @@ export class Client {
654639 this . registerPageEventListener ( 'framenavigated' , async frame => {
655640 if ( frame . url ( ) . includes ( 'post_logout=1' ) ) {
656641 console . log ( "LOGGED OUT" )
642+ log . warn ( "LOGGED OUT" )
657643 await fn ( true ) ;
658644 }
659645 } , priority )
@@ -1126,7 +1112,8 @@ public async onLiveLocation(chatId: ChatId, fn: (liveLocationChangedEvent: LiveL
11261112 public async kill ( ) : Promise < boolean > {
11271113 if ( this . _currentlyBeingKilled ) return ;
11281114 this . _currentlyBeingKilled = true ;
1129- console . log ( 'Shutting Down' ) ;
1115+ console . log ( 'Killing client. Shutting Down' ) ;
1116+ log . info ( 'Killing client. Shutting Down' )
11301117 const browser = await this ?. _page ?. browser ( )
11311118 const pid = browser ?. process ( ) ? browser ?. process ( ) ?. pid : null ;
11321119 try {
@@ -1722,6 +1709,7 @@ public async onLiveLocation(chatId: ChatId, fn: (liveLocationChangedEvent: LiveL
17221709 ) as Promise < MessageId > ;
17231710 } else {
17241711 console . log ( 'something is wrong with this giphy link' ) ;
1712+ log . error ( 'something is wrong with this giphy link' , giphyMediaUrl ) ;
17251713 return ;
17261714 }
17271715 }
@@ -2830,7 +2818,6 @@ public async getStatus(contactId: ContactId) : Promise<{
28302818 */
28312819 public async setGroupIcon ( groupId : GroupChatId , image : DataURL ) :Promise < boolean > {
28322820 const mimeInfo = base64MimeType ( image ) ;
2833- console . log ( "setGroupIcon -> mimeInfo" , mimeInfo )
28342821 if ( ! mimeInfo || mimeInfo . includes ( "image" ) ) {
28352822 let imgData ;
28362823 if ( this . _createConfig . stickerServerEndpoint ) {
@@ -3193,7 +3180,6 @@ public async getStatus(contactId: ContactId) : Promise<{
31933180 }
31943181
31953182 private async prepareWebp ( image : DataURL , stickerMetadata ?: StickerMetadata ) {
3196- // console.log("prepareWebp", image.slice(0,25))
31973183 if ( isDataURL ( image ) && ! image . includes ( "image" ) ) {
31983184 console . error ( "Not an image. Please use convertMp4BufferToWebpDataUrl to process video stickers" ) ;
31993185 return false
@@ -3279,6 +3265,7 @@ public async getStatus(contactId: ContactId) : Promise<{
32793265 } catch ( error ) {
32803266 const msg = 'Stickers have to be less than 1MB. Please lower the fps or shorten the duration using the processOptions parameter: https://open-wa.github.io/wa-automate-nodejs/classes/client.html#sendmp4assticker'
32813267 console . log ( msg )
3268+ log . warn ( msg )
32823269 throw new CustomError ( ERROR_NAME . STICKER_TOO_LARGE , msg ) ;
32833270 }
32843271 }
@@ -3805,6 +3792,7 @@ public async getStatus(contactId: ContactId) : Promise<{
38053792 return this . _registeredWebhooks [ id ] ;
38063793 }
38073794 console . log ( 'Invalid listener(s)' , events ) ;
3795+ log . warn ( 'Invalid listener(s)' , events ) ;
38083796 return false ;
38093797 }
38103798
@@ -3829,12 +3817,14 @@ public async getStatus(contactId: ContactId) : Promise<{
38293817 if ( ! this . _registeredEvListeners ) this . _registeredEvListeners = { } ;
38303818 if ( this . _registeredEvListeners [ simpleListener ] ) {
38313819 console . log ( 'Listener already registered' ) ;
3820+ log . warn ( 'Listener already registered' ) ;
38323821 return false ;
38333822 }
38343823 this . _registeredEvListeners [ simpleListener ] = await this [ simpleListener ] ( data => ev . emit ( this . getEventSignature ( simpleListener ) , this . prepEventData ( data , simpleListener ) ) ) ;
38353824 return true ;
38363825 }
38373826 console . log ( 'Invalid lisetner' , simpleListener ) ;
3827+ log . warn ( 'Invalid lisetner' , simpleListener ) ;
38383828 return false ;
38393829 }
38403830
0 commit comments