@@ -12,53 +12,107 @@ let logFactory: LoggerFactory;
1212@Injectable ( { scope : Scope . TRANSIENT } )
1313export class MozLoggerService implements LoggerService {
1414 private mozlog : MozLogger ;
15+ public name : string ;
1516
1617 constructor (
1718 configService : ConfigService ,
18- @Inject ( INQUIRER ) private parentClass : object
19+ @Inject ( INQUIRER ) parentClass : object
1920 ) {
2021 if ( ! logFactory ) {
2122 logFactory = mozlog ( configService . get ( 'log' ) ) ;
2223 }
23- this . mozlog = logFactory ( 'default' ) ;
24- if ( this . parentClass ?. constructor ?. name ) {
25- this . setContext ( this . parentClass . constructor . name ) ;
24+
25+ if ( parentClass ?. constructor ?. name ) {
26+ this . name = parentClass ?. constructor ?. name ;
27+ } else {
28+ this . name = 'default' ;
2629 }
30+
31+ this . mozlog = logFactory ( this . name ) ;
2732 }
2833
2934 public setContext ( name : string ) : void {
35+ this . name = name ;
3036 this . mozlog = logFactory ( name ) ;
3137 }
3238
3339 log ( message : any , ...optionalParams : any [ ] ) : void {
34- this . mozlog . info ( '' , { message, ...optionalParams } ) ;
40+ const { type, data } = supportLegacyFormat ( message , optionalParams ) ;
41+ this . mozlog . info ( type , data ) ;
3542 }
3643
3744 info ( message : any , ...optionalParams : any [ ] ) : void {
38- this . mozlog . info ( '' , { message, ...optionalParams } ) ;
45+ const { type, data } = supportLegacyFormat ( message , optionalParams ) ;
46+ this . mozlog . info ( type , data ) ;
3947 }
4048
4149 error ( message : any , ...optionalParams : any [ ] ) : void {
42- this . mozlog . error ( '' , { message, ...optionalParams } ) ;
50+ const { type, data } = supportLegacyFormat ( message , optionalParams ) ;
51+ this . mozlog . error ( type , data ) ;
4352 }
4453
4554 warn ( message : any , ...optionalParams : any [ ] ) : void {
46- this . mozlog . warn ( '' , { message, ...optionalParams } ) ;
55+ const { type, data } = supportLegacyFormat ( message , optionalParams ) ;
56+ this . mozlog . warn ( type , data ) ;
4757 }
4858
4959 debug ( message : any , ...optionalParams : any [ ] ) : void {
50- this . mozlog . debug ( '' , { message, ...optionalParams } ) ;
60+ const { type, data } = supportLegacyFormat ( message , optionalParams ) ;
61+ this . mozlog . debug ( type , data ) ;
5162 }
5263
5364 verbose ( message : any , ...optionalParams : any [ ] ) : void {
54- this . mozlog . verbose ( '' , { message, ...optionalParams } ) ;
65+ const { type, data } = supportLegacyFormat ( message , optionalParams ) ;
66+ this . mozlog . verbose ( type , data ) ;
5567 }
5668
5769 trace ( message : any , ...optionalParams : any [ ] ) : void {
58- this . mozlog . trace ( '' , { message, ...optionalParams } ) ;
70+ const { type, data } = supportLegacyFormat ( message , optionalParams ) ;
71+ this . mozlog . trace ( type , data ) ;
5972 }
6073
6174 warning ( message : any , ...optionalParams : any [ ] ) : void {
62- this . mozlog . warn ( '' , { message, ...optionalParams } ) ;
75+ const { type, data } = supportLegacyFormat ( message , optionalParams ) ;
76+ this . mozlog . warn ( type , data ) ;
77+ }
78+ }
79+
80+ /**
81+ * Handles legacy logging format which was intended for mozlog. Moz log calls were typically
82+ * in the form:
83+ *
84+ * > log(type, data);
85+ * > log(type)
86+ *
87+ * Where type was a discrete log type used for look up, and data was an optional dictionary
88+ * of values, a string containing a message, or an Error.
89+ *
90+ * As long as the logger is invoked in this way, this function ensures the resulting output
91+ * is compliant with what mozlog would have produced.
92+ *
93+ * @param message The message passed to the logger
94+ * @param optionalParams The remaining parameters passed to the logger
95+ * @returns { type:string, data:any } A mozlog compliant message
96+ */
97+ export function supportLegacyFormat ( message : any , optionalParams : any [ ] ) {
98+ // This is the way most mozlog calls work, e.g log('some-type', {msg:'okay', foo:'bar'});
99+ // We don't want to disrupt the output of these types of calls, since they are can be used
100+ // for analytics...
101+ if ( typeof message === 'string' && optionalParams . length <= 1 ) {
102+ return {
103+ type : message ,
104+ data : optionalParams [ 0 ] || { } ,
105+ } ;
63106 }
107+
108+ // TODO: FXA-9951 - Will prevent this kind of call from occurring by restricting logging types.
109+ // Note, this can result in messy or invalid states in bq on jsonPayload.fields. It's
110+ // best not rely on this.
111+ return {
112+ type : '' ,
113+ data : {
114+ message,
115+ ...optionalParams ,
116+ } ,
117+ } ;
64118}
0 commit comments