@@ -2,6 +2,7 @@ import { truncateString } from "./GleapHelper";
22
33export default class GleapConsoleLogManager {
44 logArray = [ ] ;
5+ disabled = false ;
56 originalConsoleLog ;
67 logMaxLength = 500 ;
78
@@ -26,16 +27,39 @@ export default class GleapConsoleLogManager {
2627 * Revert console log overwrite.
2728 */
2829 stop ( ) {
30+ this . disabled = true ;
2931 window . console = this . originalConsoleLog ;
3032 }
3133
34+ /**
35+ * Add message with log level to logs.
36+ * @param {* } message
37+ * @param {* } logLevel
38+ * @returns
39+ */
40+ addLog ( message , logLevel = "INFO" ) {
41+ if ( ! message || message . length <= 0 ) {
42+ return ;
43+ }
44+
45+ this . logArray . push ( {
46+ log : truncateString ( message , 1000 ) ,
47+ date : new Date ( ) ,
48+ priority : logLevel ,
49+ } ) ;
50+
51+ if ( this . logArray . length > this . logMaxLength ) {
52+ this . logArray . shift ( ) ;
53+ }
54+ }
55+
3256 /**
3357 * Add entry to logs.
3458 * @param {* } args
35- * @param {* } priority
59+ * @param {* } logLevel
3660 * @returns
3761 */
38- addLog ( args , priority ) {
62+ addLogWithArgs ( args , logLevel ) {
3963 if ( ! args || args . length <= 0 ) {
4064 return ;
4165 }
@@ -44,21 +68,18 @@ export default class GleapConsoleLogManager {
4468 for ( var i = 0 ; i < args . length ; i ++ ) {
4569 log += args [ i ] + " " ;
4670 }
47- this . logArray . push ( {
48- log : truncateString ( log , 1000 ) ,
49- date : new Date ( ) ,
50- priority,
51- } ) ;
5271
53- if ( this . logArray . length > this . logMaxLength ) {
54- this . logArray . shift ( ) ;
55- }
72+ this . addLog ( log , logLevel ) ;
5673 }
5774
5875 /**
5976 * Start console log overwrite.
6077 */
6178 start ( ) {
79+ if ( this . disabled ) {
80+ return ;
81+ }
82+
6283 const self = this ;
6384 window . console = ( function ( origConsole ) {
6485 if ( ! window . console || ! origConsole ) {
@@ -70,19 +91,19 @@ export default class GleapConsoleLogManager {
7091 return {
7192 ...origConsole ,
7293 log : function ( ) {
73- self . addLog ( arguments , "INFO" ) ;
94+ self . addLogWithArgs ( arguments , "INFO" ) ;
7495 origConsole . log && origConsole . log . apply ( origConsole , arguments ) ;
7596 } ,
7697 warn : function ( ) {
77- self . addLog ( arguments , "WARNING" ) ;
98+ self . addLogWithArgs ( arguments , "WARNING" ) ;
7899 origConsole . warn && origConsole . warn . apply ( origConsole , arguments ) ;
79100 } ,
80101 error : function ( ) {
81- self . addLog ( arguments , "ERROR" ) ;
102+ self . addLogWithArgs ( arguments , "ERROR" ) ;
82103 origConsole . error && origConsole . error . apply ( origConsole , arguments ) ;
83104 } ,
84105 info : function ( v ) {
85- self . addLog ( arguments , "INFO" ) ;
106+ self . addLogWithArgs ( arguments , "INFO" ) ;
86107 origConsole . info && origConsole . info . apply ( origConsole , arguments ) ;
87108 } ,
88109 } ;
0 commit comments