Skip to content

Commit b1a391f

Browse files
committed
chore: replace console.error and console.warn with afLogger for consistent logging across modules
1 parent c3ae78b commit b1a391f

File tree

13 files changed

+43
-40
lines changed

13 files changed

+43
-40
lines changed

adminforth/auth.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import jwt from 'jsonwebtoken';
33
import crypto from 'crypto';
44
import AdminForth from './index.js';
55
import { IAdminForthAuth } from './types/Back.js';
6+
import { afLogger } from './modules/logger.js';
67

78
// Function to generate a password hash using PBKDF2
89
function calcPasswordHash(password, salt, iterations = 100000, keyLength = 64, digest = 'sha512') {
@@ -105,7 +106,7 @@ class AdminForthAuth implements IAdminForthAuth {
105106
if (expirySeconds !== undefined) {
106107
expiryMs = expirySeconds * 1000;
107108
} else if (expiry !== undefined) {
108-
console.warn('setCustomCookie: expiry(in ms) is deprecated, use expirySeconds instead (seconds), traceback:', new Error().stack);
109+
afLogger.warn(`setCustomCookie: expiry(in ms) is deprecated, use expirySeconds instead (seconds), traceback: ${new Error().stack}`);
109110
expiryMs = expiry;
110111
}
111112

@@ -145,23 +146,23 @@ class AdminForthAuth implements IAdminForthAuth {
145146
decoded = jwt.verify(jwtToken, secret);
146147
} catch (err) {
147148
if (err.name === 'TokenExpiredError') {
148-
console.error('Token expired:', err.message);
149+
afLogger.error(`Token expired: ${err.message}`);
149150
} else if (err.name === 'JsonWebTokenError') {
150-
console.error('Token error:', err.message);
151+
afLogger.error(`Token error: ${err.message}`);
151152
} else {
152-
console.error('Failed to verify JWT token', err);
153+
afLogger.error(`Failed to verify JWT token: ${err}`);
153154
}
154155
return null;
155156
}
156157
const { pk, t } = decoded;
157158
if (t !== mustHaveType) {
158-
console.error(`Invalid token type during verification: ${t}, must be ${mustHaveType}`);
159+
afLogger.error(`Invalid token type during verification: ${t}, must be ${mustHaveType}`);
159160
return null;
160161
}
161162
if (decodeUser !== false) {
162163
const dbUser = await this.adminforth.getUserByPk(pk);
163164
if (!dbUser) {
164-
console.error(`User with pk ${pk} not found in database`);
165+
afLogger.error(`User with pk ${pk} not found in database`);
165166
// will logout user which was deleted
166167
return null;
167168
}

adminforth/commands/callTsProxy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function callTsProxy(tsCode, silent=false) {
5555
reject(new Error("Invalid JSON from tsproxy: " + stdout));
5656
}
5757
} else {
58-
console.error(`tsproxy exited with non-0, this should never happen, stdout: ${stdout}, stderr: ${stderr}`);
58+
afLogger.error(`tsproxy exited with non-0, this should never happen, stdout: ${stdout}, stderr: ${stderr}`);
5959
reject(new Error(stderr));
6060
}
6161
});
@@ -102,7 +102,7 @@ export async function findAdminInstance() {
102102
// and show the error so user can fix it
103103
const fileContent = fs.readFileSync(file, "utf-8");
104104
if (fileContent.includes("export const admin")) {
105-
console.error(chalk.red(`Error running ${file}:`, e));
105+
afLogger.error(`Error running ${file}: ${e}`);
106106
process.exit(1);
107107
}
108108
afLogger.trace(`🪲 File ${file} failed`, e);

adminforth/dataConnectors/baseConnector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
1818
client: any;
1919

2020
get db() {
21-
console.warn('.db is deprecated, use .client instead');
21+
afLogger.warn('.db is deprecated, use .client instead');
2222
return this.client;
2323
}
2424

adminforth/dataConnectors/clickhouse.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import dayjs from 'dayjs';
44
import { createClient } from '@clickhouse/client'
55

66
import { AdminForthDataTypes, AdminForthFilterOperators, AdminForthSortDirections } from '../types/Common.js';
7+
import { afLogger } from '../modules/logger.js';
78

89
class ClickhouseConnector extends AdminForthBaseConnector implements IAdminForthDataSourceConnector {
910

@@ -95,7 +96,7 @@ class ClickhouseConnector extends AdminForthBaseConnector implements IAdminForth
9596
});
9697
rows = await q.json();
9798
} catch (e) {
98-
console.error(` 🛑Error connecting to datasource URL ${this.url}:`, e);
99+
afLogger.error(` 🛑Error connecting to datasource URL ${this.url}: ${e}`);
99100
return null;
100101
}
101102

@@ -169,7 +170,7 @@ class ClickhouseConnector extends AdminForthBaseConnector implements IAdminForth
169170
return {'error': `Failed to parse JSON: ${e.message}`}
170171
}
171172
} else {
172-
console.error(`AdminForth: JSON field is not a string but ${field._underlineType}, this is not supported yet`);
173+
afLogger.error(`AdminForth: JSON field is not a string but ${field._underlineType}, this is not supported yet`);
173174
}
174175
}
175176
return value;
@@ -197,7 +198,7 @@ class ClickhouseConnector extends AdminForthBaseConnector implements IAdminForth
197198
if (field._underlineType.startsWith('String') || field._underlineType.startsWith('FixedString')) {
198199
return JSON.stringify(value);
199200
} else {
200-
console.error(`AdminForth: JSON field is not a string/text but ${field._underlineType}, this is not supported yet`);
201+
afLogger.error(`AdminForth: JSON field is not a string/text but ${field._underlineType}, this is not supported yet`);
201202
}
202203
}
203204

adminforth/dataConnectors/mongo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
262262

263263
// explicitly ignore raw SQL filters for MongoDB
264264
if ((filter as IAdminForthSingleFilter).insecureRawSQL !== undefined) {
265-
console.warn('⚠️ Ignoring insecureRawSQL filter for MongoDB:', (filter as IAdminForthSingleFilter).insecureRawSQL);
265+
afLogger.warn(`⚠️ Ignoring insecureRawSQL filter for MongoDB:, ${(filter as IAdminForthSingleFilter).insecureRawSQL}`);
266266
return {};
267267
}
268268

adminforth/dataConnectors/mysql.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AdminForthResource, IAdminForthSingleFilter, IAdminForthAndOrFilter, IA
33
import { AdminForthDataTypes, AdminForthFilterOperators, AdminForthSortDirections, } from '../types/Common.js';
44
import AdminForthBaseConnector from './baseConnector.js';
55
import mysql from 'mysql2/promise';
6-
import { dbLogger } from '../modules/logger.js';
6+
import { dbLogger, afLogger } from '../modules/logger.js';
77

88
class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataSourceConnector {
99

@@ -16,7 +16,7 @@ class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataS
1616
queueLimit: 0
1717
});
1818
} catch (e) {
19-
console.error(`Failed to connect to MySQL: ${e}`);
19+
afLogger.error(`Failed to connect to MySQL: ${e}`);
2020
}
2121
}
2222

@@ -176,8 +176,8 @@ class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataS
176176
} else if (typeof value === 'object') {
177177
return value;
178178
} else {
179-
console.error('JSON field value is not string or object, but has type:', typeof value);
180-
console.error('Field:', field);
179+
afLogger.error(`JSON field value is not string or object, but has type: ${typeof value}`);
180+
afLogger.error(`Field:, ${field}`);
181181
return {}
182182
}
183183
}

adminforth/dataConnectors/postgres.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
2323
this.setupClient(url);
2424
});
2525
} catch (e) {
26-
console.error(`Failed to connect to Postgres ${e}`);
26+
afLogger.error(`Failed to connect to Postgres ${e}`);
2727
}
2828
}
2929

@@ -200,8 +200,8 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
200200
} else if (typeof value == 'object') {
201201
return value;
202202
} else {
203-
console.error('JSON field value is not string or object, but has type:', typeof value);
204-
console.error('Field:', field);
203+
afLogger.error(`JSON field value is not string or object, but has type: ${typeof value}`);
204+
afLogger.error(`Field:, ${field}`);
205205
return {}
206206
}
207207
}

adminforth/dataConnectors/sqlite.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IAdminForthDataSourceConnector, IAdminForthSingleFilter, IAdminForthAnd
33
import AdminForthBaseConnector from './baseConnector.js';
44
import dayjs from 'dayjs';
55
import { AdminForthDataTypes, AdminForthFilterOperators, AdminForthSortDirections } from '../types/Common.js';
6-
import { dbLogger } from '../modules/logger.js';
6+
import { dbLogger, afLogger } from '../modules/logger.js';
77

88
class SQLiteConnector extends AdminForthBaseConnector implements IAdminForthDataSourceConnector {
99

@@ -123,7 +123,7 @@ class SQLiteConnector extends AdminForthBaseConnector implements IAdminForthData
123123
return {'error': `Failed to parse JSON: ${e.message}`}
124124
}
125125
} else {
126-
console.error(`AdminForth: JSON field is not a string/text but ${field._underlineType}, this is not supported yet`);
126+
afLogger.error(`AdminForth: JSON field is not a string/text but ${field._underlineType}, this is not supported yet`);
127127
}
128128
}
129129

@@ -157,7 +157,7 @@ class SQLiteConnector extends AdminForthBaseConnector implements IAdminForthData
157157
if (field._underlineType == 'text' || field._underlineType == 'varchar') {
158158
return JSON.stringify(value);
159159
} else {
160-
console.error(`AdminForth: JSON field is not a string/text but ${field._underlineType}, this is not supported yet`);
160+
afLogger.error(`AdminForth: JSON field is not a string/text but ${field._underlineType}, this is not supported yet`);
161161
}
162162
}
163163

adminforth/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ class AdminForth implements IAdminForth {
384384
try {
385385
await this.connectors[dataSourceId].setupClient(this.config.dataSources.find((ds) => ds.id === dataSourceId).url);
386386
} catch (e) {
387-
console.error(`Error while connecting to datasource '${dataSourceId}':`, e);
387+
afLogger.error(`Error while connecting to datasource '${dataSourceId}': ${e}`);
388388
}
389389
}));
390390

@@ -399,13 +399,13 @@ class AdminForth implements IAdminForth {
399399
try {
400400
fieldTypes = await this.connectors[res.dataSource].discoverFields(res);
401401
} catch (e) {
402-
console.error(`Error discovering fields for resource '${res.table}' (In resource '${res.resourceId}')`, e);
402+
afLogger.error(`Error discovering fields for resource '${res.table}' (In resource '${res.resourceId}') ${e}`);
403403
}
404404
if (fieldTypes !== null && !Object.keys(fieldTypes).length) {
405405
throw new Error(`Table '${res.table}' (In resource '${res.resourceId}') has no fields or does not exist`);
406406
}
407407
if (fieldTypes === null) {
408-
console.error(`⛔ DataSource ${res.dataSource} was not able to perform field discovery. It will not work properly`);
408+
afLogger.error(`⛔ DataSource ${res.dataSource} was not able to perform field discovery. It will not work properly`);
409409
if (process.env.NODE_ENV === 'production') {
410410
process.exit(1);
411411
}
@@ -458,7 +458,7 @@ class AdminForth implements IAdminForth {
458458
const tables = await connector.getAllTables();
459459
results[dataSourceId] = tables;
460460
} catch (err) {
461-
console.error(`Error getting tables for dataSource ${dataSourceId}:`, err);
461+
afLogger.error(`Error getting tables for dataSource ${dataSourceId}: ${err}`);
462462
results[dataSourceId] = [];
463463
}
464464
} else {
@@ -489,7 +489,7 @@ class AdminForth implements IAdminForth {
489489
isUUID: isProbablyUUIDColumn(column),
490490
}));
491491
} catch (err) {
492-
console.error(`Error getting columns for table ${tableName} in dataSource ${dataSourceId}:`, err);
492+
afLogger.error(`Error getting columns for table ${tableName} in dataSource ${dataSourceId}: ${err}`);
493493
results[dataSourceId] = [];
494494
}
495495
} else {
@@ -631,7 +631,7 @@ class AdminForth implements IAdminForth {
631631
}
632632

633633
if (record) {
634-
console.warn(`updateResourceRecord function received 'record' param which is deprecated and will be removed in future version, please use 'updates' instead.`);
634+
afLogger.warn(`updateResourceRecord function received 'record' param which is deprecated and will be removed in future version, please use 'updates' instead.`);
635635
}
636636

637637
// remove editReadonly columns from record

adminforth/modules/codeInjector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -965,8 +965,8 @@ class CodeInjector implements ICodeInjector {
965965
}
966966
});
967967
devServer.stderr.on('data', (data) => {
968-
console.error(`[AdminForth SPA ERROR]:`);
969-
afLogger.trace(data.toString());
968+
afLogger.error(`[AdminForth SPA ERROR]:`);
969+
afLogger.error(data.toString());
970970
});
971971

972972
}

0 commit comments

Comments
 (0)