You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constlastUpdatedListAt=newDate(1999);// some date that's definitely past
31
-
letextendedLogging=false;
32
44
letuseBotblockAPI=true;
33
45
46
+
letlogLevel=LogLevel.WarnAndErrorOnly;
47
+
34
48
/**
35
49
* the userLogger variable will later be defined with the
36
50
* logger supplied by the user if they supplied any
37
51
*/
38
52
// eslint-disable-next-line max-len
39
53
letuserLogger: UserLogger|undefined;
40
54
41
-
constlog={
42
-
info: (msg: string)=>(userLogger
43
-
? userLogger.info(`BLAPI: ${msg}`)
44
-
: console.info(`[INFO] BLAPI: ${msg}`)),
45
-
warn: (msg: string)=>(userLogger
46
-
? userLogger.warn(`BLAPI: ${msg}`)
47
-
: console.warn(`[WARN] BLAPI: ${msg}`)),
48
-
error: (err: any)=>(userLogger
49
-
? userLogger.error(`BLAPI: ${err}`)
50
-
: console.error(`[ERROR] BLAPI ${err}`)),
55
+
functioncreateBlapiMessage(message: string){
56
+
return`BLAPI: ${message}`;
57
+
}
58
+
59
+
constlogger={
60
+
info: (msg: string)=>{
61
+
if(logLevel<LogLevel.All){
62
+
return;
63
+
}
64
+
if(userLogger){
65
+
userLogger.info(createBlapiMessage(msg));
66
+
}else{
67
+
console.info(createBlapiMessage(msg));
68
+
}
69
+
},
70
+
warn: (msg: string)=>{
71
+
if(logLevel<LogLevel.WarnAndErrorOnly){
72
+
return;
73
+
}
74
+
if(userLogger){
75
+
userLogger.warn(createBlapiMessage(msg));
76
+
}else{
77
+
console.warn(createBlapiMessage(msg));
78
+
}
79
+
},
80
+
error: (msg: string)=>{
81
+
if(logLevel<LogLevel.ErrorOnly){
82
+
return;
83
+
}
84
+
if(userLogger){
85
+
userLogger.error(createBlapiMessage(msg));
86
+
}else{
87
+
console.error(createBlapiMessage(msg));
88
+
}
89
+
},
51
90
};
52
91
53
92
functionconvertLegacyIds(apiKeys: apiKeysObject){
@@ -100,36 +139,36 @@ async function postToAllLists(
100
139
try{
101
140
consttmpListData=awaitget<listDataType>(
102
141
'https://botblock.org/api/lists?filter=true',
103
-
log,
142
+
logger,
104
143
);
105
144
// make sure we only save it if nothing goes wrong
106
145
if(tmpListData){
107
146
listData=tmpListData;
108
-
log.info('Updated list endpoints.');
147
+
logger.info('Updated list endpoints.');
109
148
}else{
110
-
log.error('Got empty list of endpoints from botblock.');
149
+
logger.error('Got empty list of endpoints from botblock.');
111
150
}
112
151
}catch(e){
113
-
log.error(e);
114
-
log.error(
152
+
logger.error(String(e));
153
+
logger.error(
115
154
"Something went wrong when contacting BotBlock for the API of the lists, so we're using an older preset. Some lists might not be available because of this.",
116
155
);
117
156
}
118
157
try{
119
158
consttmpLegacyIdsData=awaitget<legacyIdDataType>(
120
159
'https://botblock.org/api/legacy-ids',
121
-
log,
160
+
logger,
122
161
);
123
162
// make sure we only save it if nothing goes wrong
124
163
if(tmpLegacyIdsData){
125
164
legacyIds=tmpLegacyIdsData;
126
-
log.info('Updated legacy Ids.');
165
+
logger.info('Updated legacy Ids.');
127
166
}else{
128
-
log.error('Got empty list of legacy Ids from botblock.');
167
+
logger.error('Got empty list of legacy Ids from botblock.');
129
168
}
130
169
}catch(e){
131
-
log.error(e);
132
-
log.error(
170
+
logger.error(String(e));
171
+
logger.error(
133
172
"Something went wrong when contacting BotBlock for legacy Ids, so we're using an older preset. Some lists might not be available because of this.",
134
173
);
135
174
}
@@ -158,9 +197,7 @@ async function postToAllLists(
constlogger=logOptions.logger!;// we checked that it exists beforehand
439
+
constpassedLogger=logOptions.logger!;// we checked that it exists beforehand
401
440
// making sure the logger supplied by the user has our required log levels (info, warn, error)
402
441
if(
403
-
typeoflogger.info!=='function'
404
-
||typeoflogger.warn!=='function'
405
-
||typeoflogger.error!=='function'
442
+
typeofpassedLogger.info!=='function'
443
+
||typeofpassedLogger.warn!=='function'
444
+
||typeofpassedLogger.error!=='function'
406
445
){
407
446
thrownewError(
408
447
'Your supplied logger does not seem to expose the log levels BLAPI needs to work. Make sure your logger offers the following methods: info() warn() error()',
0 commit comments