Skip to content

Commit

Permalink
fix: cron application not logging to console (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustSamuel authored Feb 8, 2025
1 parent 1e02f0b commit f2ad374
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
3 changes: 2 additions & 1 deletion init_scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ chmod +x /app/init_scripts/00_make_sudosos_data_dirs.sh
chmod +x /app/init_scripts/00_regen_sudosos_secrets.sh
sh /app/init_scripts/00_make_sudosos_data_dirs.sh
sh /app/init_scripts/00_regen_sudosos_secrets.sh
pm2 start /app/pm2.json --attach
pm2 start /app/pm2.json
pm2 logs
3 changes: 2 additions & 1 deletion src/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import LdapSyncService from './service/sync/user/ldap-sync-service';
import { UserSyncService } from './service/sync/user/user-sync-service';
import UserSyncManager from './service/sync/user/user-sync-manager';
import GewisDBSyncService from './gewis/service/gewisdb-sync-service';
import getAppLogger from './helpers/logging';

class CronApplication {
logger: Logger;
Expand All @@ -62,7 +63,7 @@ async function createCronTasks(): Promise<void> {
application.logger.level = process.env.LOG_LEVEL;
application.logger.info('Starting cron tasks...');

const logger = log4js.getLogger('Console (cron)');
const logger = getAppLogger('Console (cron)');
logger.level = process.env.LOG_LEVEL;
console.log = (message: any, ...additional: any[]) => logger.debug(message, ...additional);

Expand Down
35 changes: 35 additions & 0 deletions src/helpers/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/

import log4js from 'log4js';

export default function getAppLogger(category: string = 'Application'): log4js.Logger {
log4js.configure({
pm2: true,
appenders: {
out: { type: 'stdout' },
},
disableClustering: true,
categories: { default: { appenders: ['out'], level: 'all' } },
});
const logger = log4js.getLogger(category);
logger.level = process.env.LOG_LEVEL;
return logger;
}
12 changes: 2 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import SellerPayoutController from './controller/seller-payout-controller';
import { ISettings } from './entity/server-setting';
import ServerSettingsController from './controller/server-settings-controller';
import TransactionSummaryController from './controller/transaction-summary-controller';
import getAppLogger from './helpers/logging';

export class Application {
app: express.Express;
Expand Down Expand Up @@ -162,16 +163,7 @@ async function setupAuthentication(tokenHandler: TokenHandler, application: Appl

export default async function createApp(): Promise<Application> {
const application = new Application();
log4js.configure({
pm2: true,
appenders: {
out: { type: 'stdout' },
},
disableClustering: true,
categories: { default: { appenders: ['out'], level: 'all' } },
});
application.logger = log4js.getLogger('Application');
application.logger.level = process.env.LOG_LEVEL;
application.logger = getAppLogger();
application.logger.info('Starting application instance...');

// Validate environment variables
Expand Down

0 comments on commit f2ad374

Please sign in to comment.