Skip to content

Commit

Permalink
fix: configure logging with log4js
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Feb 23, 2024
1 parent c87932e commit 65b106a
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 33 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
},
"dependencies": {
"fast-xml-parser": "^4.3.4",
"log4js": "^6.9.1",
"tslib": "^2.6.2"
}
}
75 changes: 70 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
import * as log4js from "log4js";

export { ReassembleXMLFileHandler } from "./service/reassembleXMLFileHandler";
export { DisassembleXMLFileHandler } from "./service/disassembleXMLFileHandler";

// Function to update the log level
export function setLogLevel(level: string) {
log4js.getLogger().level = level;
}

// Expose the logger
export const logger = log4js.getLogger();

log4js.configure({
appenders: { disassemble: { type: "file", filename: "disassemble.log" } },
categories: { default: { appenders: ["disassemble"], level: "error" } },
});
5 changes: 3 additions & 2 deletions src/service/buildDisassembledFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import * as fs from "node:fs";
import * as path from "node:path";
import { logger } from "@src/index";
import { XMLParser } from "fast-xml-parser";

import { XML_HEADER } from "@src/helpers/constants";
Expand Down Expand Up @@ -89,7 +90,7 @@ export function buildDisassembledFiles(
const leafOutputPath = path.join(metadataPath, `${baseName}.xml`);
fs.writeFileSync(leafOutputPath, leafFile);

console.log(`All leaf elements saved to: ${leafOutputPath}`);
logger.debug(`All leaf elements saved to: ${leafOutputPath}`);
}
}

Expand Down Expand Up @@ -128,5 +129,5 @@ function buildNestedFile(

// Write the XML content to the determined output path
fs.writeFileSync(outputPath, decomposeFileContents);
console.log(`XML content saved to: ${outputPath}`);
logger.debug(`XML content saved to: ${outputPath}`);
}
3 changes: 2 additions & 1 deletion src/service/buildReassembledFiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

import * as fs from "node:fs/promises";
import { logger } from "@src/index";
import { XML_HEADER, INDENT } from "@src/helpers/constants";

export async function buildReassembledFile(
Expand Down Expand Up @@ -54,5 +55,5 @@ export async function buildReassembledFile(
filePath,
`${XML_HEADER}\n${openTag}${finalXmlContent}${closeTag}`,
);
console.log(`Created reassembled file: ${filePath}`);
logger.debug(`Created reassembled file: ${filePath}`);
}
4 changes: 3 additions & 1 deletion src/service/disassembleXMLFileHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { logger } from "@src/index";
import { INDENT } from "@src/helpers/constants";
import { buildDisassembledFiles } from "@src/service/buildDisassembledFiles";

Expand All @@ -13,6 +14,7 @@ export class DisassembleXMLFileHandler {
const fileStat = await fs.stat(xmlPath);

if (!fileStat.isDirectory()) {
logger.error("The provided xmlPath is not a directory.");
throw new Error("The provided xmlPath is not a directory.");
}
const files = await fs.readdir(xmlPath);
Expand All @@ -36,7 +38,7 @@ export class DisassembleXMLFileHandler {
const { xmlPath, filePath, uniqueIdElements, purge } = xmlAttributes;

if (filePath.endsWith(".xml")) {
console.log(`Parsing file: ${filePath}`);
logger.debug(`Parsing file: ${filePath}`);
const xmlContent = await fs.readFile(filePath, "utf-8");
const fullName = path.basename(filePath, path.extname(filePath));
const baseName = fullName.split(".")[0];
Expand Down
2 changes: 2 additions & 0 deletions src/service/reassembleXMLFileHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { logger } from "@src/index";
import { XMLParser } from "fast-xml-parser";
import { XmlElement, XML_PARSER_OPTION } from "@src/helpers/types";
import { buildReassembledFile } from "@src/service/buildReassembledFiles";
Expand Down Expand Up @@ -83,6 +84,7 @@ export class ReassembleXMLFileHandler {
const fileStat = await fs.stat(xmlPath);

if (!fileStat.isDirectory()) {
logger.error("The provided xmlPath is not a directory.");
throw new Error("The provided xmlPath is not a directory.");
}
// Process files directly inside the `xmlPath` directory
Expand Down
Loading

0 comments on commit 65b106a

Please sign in to comment.