chore(backend): Switch console naar logger

Maak gebruik van custom logger
This commit is contained in:
Tibo De Peuter 2025-03-02 15:14:26 +01:00
parent f82668148c
commit c2e3886f3f
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
6 changed files with 29 additions and 13 deletions

View file

@ -6,6 +6,7 @@ import {
} from '../services/learningObjects.js'; } from '../services/learningObjects.js';
import { FALLBACK_LANG } from '../config.js'; import { FALLBACK_LANG } from '../config.js';
import { FilteredLearningObject } from '../interfaces/learningPath'; import { FilteredLearningObject } from '../interfaces/learningPath';
import { getLogger } from '../logging/initalize';
export async function getAllLearningObjects( export async function getAllLearningObjects(
req: Request, req: Request,
@ -33,7 +34,7 @@ export async function getAllLearningObjects(
res.json(learningObjects); res.json(learningObjects);
} catch (error) { } catch (error) {
console.error('Error fetching learning objects:', error); getLogger().error('Error fetching learning objects:', error);
res.status(500).json({ error: 'Internal server error' }); res.status(500).json({ error: 'Internal server error' });
} }
} }
@ -54,7 +55,7 @@ export async function getLearningObject(
const learningObject = await getLearningObjectById(hruid, language); const learningObject = await getLearningObjectById(hruid, language);
res.json(learningObject); res.json(learningObject);
} catch (error) { } catch (error) {
console.error('Error fetching learning object:', error); getLogger().error('Error fetching learning object:', error);
res.status(500).json({ error: 'Internal server error' }); res.status(500).json({ error: 'Internal server error' });
} }
} }

View file

@ -5,6 +5,7 @@ import {
fetchLearningPaths, fetchLearningPaths,
searchLearningPaths, searchLearningPaths,
} from '../services/learningPaths.js'; } from '../services/learningPaths.js';
import { getLogger } from '../logging/initalize.js';
/** /**
* Fetch learning paths based on query parameters. * Fetch learning paths based on query parameters.
*/ */
@ -56,7 +57,7 @@ export async function getLearningPaths(
); );
res.json(learningPaths.data); res.json(learningPaths.data);
} catch (error) { } catch (error) {
console.error('❌ Unexpected error fetching learning paths:', error); getLogger().error('❌ Unexpected error fetching learning paths:', error);
res.status(500).json({ error: 'Internal server error' }); res.status(500).json({ error: 'Internal server error' });
} }
} }

View file

@ -1,9 +1,13 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import yaml from 'js-yaml'; import yaml from 'js-yaml';
import { Logger } from 'winston';
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { themes } from '../data/themes.js'; import { themes } from '../data/themes.js';
import { FALLBACK_LANG } from '../config.js'; import { FALLBACK_LANG } from '../config.js';
import { getLogger } from '../logging/initalize.js';
const logger: Logger = getLogger();
interface Translations { interface Translations {
curricula_page: { curricula_page: {
@ -17,10 +21,10 @@ function loadTranslations(language: string): Translations {
const yamlFile = fs.readFileSync(filePath, 'utf8'); const yamlFile = fs.readFileSync(filePath, 'utf8');
return yaml.load(yamlFile) as Translations; return yaml.load(yamlFile) as Translations;
} catch (error) { } catch (error) {
console.error( logger.error(
`Cannot load translation for: ${language}, fallen back to Dutch` `Cannot load translation for: ${language}, fallen back to Dutch`
); );
console.error(error); logger.error(error);
const fallbackPath = path.join(process.cwd(), '_i18n', 'nl.yml'); const fallbackPath = path.join(process.cwd(), '_i18n', 'nl.yml');
return yaml.load(fs.readFileSync(fallbackPath, 'utf8')) as Translations; return yaml.load(fs.readFileSync(fallbackPath, 'utf8')) as Translations;
} }

View file

@ -7,6 +7,10 @@ import {
LearningPathResponse, LearningPathResponse,
} from '../interfaces/learningPath.js'; } from '../interfaces/learningPath.js';
import { fetchLearningPaths } from './learningPaths.js'; import { fetchLearningPaths } from './learningPaths.js';
import { getLogger } from '../logging/initalize.js';
import { Logger } from 'winston';
const logger: Logger = getLogger();
function filterData( function filterData(
data: LearningObjectMetadata, data: LearningObjectMetadata,
@ -49,7 +53,7 @@ export async function getLearningObjectById(
); );
if (!metadata) { if (!metadata) {
console.error(`⚠️ WARNING: Learning object "${hruid}" not found.`); logger.error(`⚠️ WARNING: Learning object "${hruid}" not found.`);
return null; return null;
} }
@ -77,7 +81,7 @@ async function fetchLearningObjects(
!learningPathResponse.success || !learningPathResponse.success ||
!learningPathResponse.data?.length !learningPathResponse.data?.length
) { ) {
console.error( logger.error(
`⚠️ WARNING: Learning path "${hruid}" exists but contains no learning objects.` `⚠️ WARNING: Learning path "${hruid}" exists but contains no learning objects.`
); );
return []; return [];
@ -104,7 +108,7 @@ async function fetchLearningObjects(
}); });
}); });
} catch (error) { } catch (error) {
console.error('❌ Error fetching learning objects:', error); logger.error('❌ Error fetching learning objects:', error);
return []; return [];
} }
} }

View file

@ -4,6 +4,10 @@ import {
LearningPath, LearningPath,
LearningPathResponse, LearningPathResponse,
} from '../interfaces/learningPath.js'; } from '../interfaces/learningPath.js';
import { getLogger } from '../logging/initalize.js';
import { Logger } from 'winston';
const logger: Logger = getLogger();
export async function fetchLearningPaths( export async function fetchLearningPaths(
hruids: string[], hruids: string[],
@ -29,7 +33,7 @@ export async function fetchLearningPaths(
); );
if (!learningPaths || learningPaths.length === 0) { if (!learningPaths || learningPaths.length === 0) {
console.error(`⚠️ WARNING: No learning paths found for ${source}.`); logger.error(`⚠️ WARNING: No learning paths found for ${source}.`);
return { return {
success: false, success: false,
source, source,

View file

@ -1,6 +1,8 @@
import axios, { AxiosRequestConfig } from 'axios'; import axios, { AxiosRequestConfig } from 'axios';
import { getLogger } from '../logging/initalize.js';
import { Logger } from 'winston';
// !!!! when logger is done -> change const logger: Logger = getLogger();
/** /**
* Utility function to fetch data from an API endpoint with error handling. * Utility function to fetch data from an API endpoint with error handling.
@ -24,16 +26,16 @@ export async function fetchWithLogging<T>(
} catch (error: any) { } catch (error: any) {
if (error.response) { if (error.response) {
if (error.response.status === 404) { if (error.response.status === 404) {
console.error( logger.error(
`❌ ERROR: ${description} not found (404) at "${url}".` `❌ ERROR: ${description} not found (404) at "${url}".`
); );
} else { } else {
console.error( logger.error(
`❌ ERROR: Failed to fetch ${description}. Status: ${error.response.status} - ${error.response.statusText} (URL: "${url}")` `❌ ERROR: Failed to fetch ${description}. Status: ${error.response.status} - ${error.response.statusText} (URL: "${url}")`
); );
} }
} else { } else {
console.error( logger.error(
`❌ ERROR: Network or unexpected error when fetching ${description}:`, `❌ ERROR: Network or unexpected error when fetching ${description}:`,
error.message error.message
); );