Skip to content

Commit a82167d

Browse files
committed
Refactor logging system: replace loglevel with custom logging functions and update related imports
1 parent 3531e78 commit a82167d

File tree

6 files changed

+73
-70
lines changed

6 files changed

+73
-70
lines changed

NOTICE.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,4 @@ FEAScript makes use of the following third-party software:
88
2. **plotly.js**
99
- License: MIT License
1010
- Source: https://github.com/plotly/plotly.js/tree/master
11-
- License: https://github.com/plotly/plotly.js/blob/master/LICENSE
12-
13-
2. **loglevel**
14-
- License: MIT License
15-
- Source: https://github.com/pimterry/loglevel
16-
- License: https://github.com/pimterry/loglevel/blob/main/LICENSE-MIT
11+
- License: https://github.com/plotly/plotly.js/blob/master/LICENSE

src/FEAScript.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// Website: https://feascript.com/ \__| //
1010

1111
import { assembleSolidHeatTransferMat } from "./solvers/solidHeatTransferScript.js";
12+
import { basicLog, debugLog } from "./utilities/utilitiesScript.js";
1213

1314
/**
1415
* FEAScript: An open-source finite element simulation library developed in JavaScript
@@ -23,27 +24,27 @@ export class FEAScriptModel {
2324
this.meshConfig = {};
2425
this.boundaryConditions = {};
2526
this.solverMethod = "lusolve"; // Default solver method
26-
console.log("FEAScriptModel instance created");
27+
basicLog("FEAScriptModel instance created");
2728
}
2829

2930
setSolverConfig(solverConfig) {
3031
this.solverConfig = solverConfig;
31-
console.log(`Solver config set to: ${solverConfig}`);
32+
debugLog(`Solver config set to: ${solverConfig}`);
3233
}
3334

3435
setMeshConfig(meshConfig) {
3536
this.meshConfig = meshConfig;
36-
console.log(`Mesh config set with dimensions: ${meshConfig.meshDimension}, elements: ${meshConfig.numElementsX}x${meshConfig.numElementsY || 1}`);
37+
debugLog(`Mesh config set with dimensions: ${meshConfig.meshDimension}, elements: ${meshConfig.numElementsX}x${meshConfig.numElementsY || 1}`);
3738
}
3839

3940
addBoundaryCondition(boundaryKey, condition) {
4041
this.boundaryConditions[boundaryKey] = condition;
41-
console.log(`Boundary condition added for key: ${boundaryKey}, type: ${condition[0]}`);
42+
debugLog(`Boundary condition added for boundary: ${boundaryKey}, type: ${condition[0]}`);
4243
}
4344

4445
setSolverMethod(solverMethod) {
4546
this.solverMethod = solverMethod;
46-
console.log(`Solver method set to: ${solverMethod}`);
47+
debugLog(`Solver method set to: ${solverMethod}`);
4748
}
4849

4950
solve() {
@@ -53,37 +54,33 @@ export class FEAScriptModel {
5354
throw new Error(error);
5455
}
5556

56-
let jacobianMatrix = []; // Jacobian matrix
57-
let residualVector = []; // Galerkin residuals
58-
let solutionVector = []; // Solution vector
59-
let nodesCoordinates = {}; // Object to store x and y coordinates of nodes
57+
let jacobianMatrix = [];
58+
let residualVector = [];
59+
let solutionVector = [];
60+
let nodesCoordinates = {};
6061

6162
// Assembly matrices
62-
console.log("Beginning matrix assembly...");
63+
basicLog("Beginning matrix assembly...");
6364
console.time("assemblyMatrices");
6465
if (this.solverConfig === "solidHeatTransferScript") {
65-
console.log(`Using solver: ${this.solverConfig}`);
66+
basicLog(`Using solver: ${this.solverConfig}`);
6667
({ jacobianMatrix, residualVector, nodesCoordinates } = assembleSolidHeatTransferMat(
6768
this.meshConfig,
6869
this.boundaryConditions
6970
));
7071
}
7172
console.timeEnd("assemblyMatrices");
72-
console.log("Matrix assembly completed");
73+
basicLog("Matrix assembly completed");
7374

7475
// System solving
75-
console.log(`Solving system using ${this.solverMethod}...`);
76+
basicLog(`Solving system using ${this.solverMethod}...`);
7677
console.time("systemSolving");
7778
if (this.solverMethod === "lusolve") {
78-
solutionVector = math.lusolve(jacobianMatrix, residualVector); // Solve the system of linear equations using LU decomposition
79+
solutionVector = math.lusolve(jacobianMatrix, residualVector);
7980
}
8081
console.timeEnd("systemSolving");
81-
console.log("System solved successfully");
82+
basicLog("System solved successfully");
8283

83-
// Return the solution matrix and nodes coordinates
84-
return {
85-
solutionVector,
86-
nodesCoordinates,
87-
};
84+
return { solutionVector, nodesCoordinates };
8885
}
8986
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
export { FEAScriptModel } from "./FEAScript.js";
1212
export { plotSolution } from "./visualization/plotSolutionScript.js";
13-
export { printVersion, setLogLevel } from "./utilities/utilitiesScript.js";
13+
export { printVersion, logSystem } from "./utilities/utilitiesScript.js";

src/solvers/solidHeatTransferScript.js

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { basisFunctions } from "../mesh/basisFunctionsScript.js";
1212
import { numericalIntegration } from "../methods/numericalIntegrationScript.js";
1313
import { meshGeneration } from "../mesh/meshGenerationScript.js";
1414
import { ThermalBoundaryConditions } from "../methods/thermalBoundaryConditionsScript.js";
15+
import { basicLog, debugLog } from "../utilities/utilitiesScript.js";
1516

1617
/**
1718
* Assemble the solid heat transfer matrix
@@ -23,7 +24,7 @@ import { ThermalBoundaryConditions } from "../methods/thermalBoundaryConditionsS
2324
* - nodesCoordinates: Object containing x and y coordinates of nodes
2425
*/
2526
export function assembleSolidHeatTransferMat(meshConfig, boundaryConditions) {
26-
console.log("Starting solid heat transfer matrix assembly");
27+
basicLog("Starting solid heat transfer matrix assembly");
2728

2829
// Extract mesh details from the configuration object
2930
const {
@@ -35,11 +36,7 @@ export function assembleSolidHeatTransferMat(meshConfig, boundaryConditions) {
3536
elementOrder, // The order of elements
3637
} = meshConfig;
3738

38-
console.log(
39-
`Mesh configuration: ${meshDimension}, Elements: ${numElementsX}x${numElementsY || 1}, Size: ${maxX}x${
40-
maxY || 0
41-
}, Order: ${elementOrder}`
42-
);
39+
debugLog(`Mesh configuration: ${meshDimension}, Elements: ${numElementsX}x${numElementsY || 1}, Size: ${maxX}x${maxY || 0}, Order: ${elementOrder}`);
4340

4441
// Extract boundary conditions from the configuration object
4542
let convectionHeatTranfCoeff = [];
@@ -49,14 +46,11 @@ export function assembleSolidHeatTransferMat(meshConfig, boundaryConditions) {
4946
if (boundaryCondition[0] === "convection") {
5047
convectionHeatTranfCoeff[key] = boundaryCondition[1];
5148
convectionExtTemp[key] = boundaryCondition[2];
52-
console.log(
53-
`Convection boundary condition on boundary ${key}: h=${boundaryCondition[1]}, T=${boundaryCondition[2]}`
54-
);
5549
}
5650
});
5751

5852
// Create a new instance of the meshGeneration class
59-
console.log("Generating mesh...");
53+
debugLog("Generating mesh...");
6054
const meshGenerationData = new meshGeneration({
6155
numElementsX,
6256
numElementsY,
@@ -68,7 +62,6 @@ export function assembleSolidHeatTransferMat(meshConfig, boundaryConditions) {
6862

6963
// Generate the mesh
7064
const nodesCoordinatesAndNumbering = meshGenerationData.generateMesh();
71-
console.log("Mesh generated successfully");
7265

7366
// Extract nodes coordinates and nodal numbering (NOP) from the mesh data
7467
let nodesXCoordinates = nodesCoordinatesAndNumbering.nodesXCoordinates;
@@ -81,9 +74,6 @@ export function assembleSolidHeatTransferMat(meshConfig, boundaryConditions) {
8174
// Initialize variables for matrix assembly
8275
const totalElements = numElementsX * (meshDimension === "2D" ? numElementsY : 1); // Total number of elements
8376
const totalNodes = totalNodesX * (meshDimension === "2D" ? totalNodesY : 1); // Total number of nodes
84-
console.log(`Total elements: ${totalElements}, Total nodes: ${totalNodes}`);
85-
86-
// Initialize variables for matrix assembly
8777
let localNodalNumbers = []; // Local nodal numbering
8878
let gaussPoints = []; // Gauss points
8979
let gaussWeights = []; // Gauss weights
@@ -112,14 +102,12 @@ export function assembleSolidHeatTransferMat(meshConfig, boundaryConditions) {
112102
}
113103

114104
// Initialize the basisFunctions class
115-
console.log("Initializing basis functions...");
116105
const basisFunctionsData = new basisFunctions({
117106
meshDimension,
118107
elementOrder,
119108
});
120109

121110
// Initialize the numericalIntegration class
122-
console.log("Setting up numerical integration...");
123111
const numIntegrationData = new numericalIntegration({
124112
meshDimension,
125113
elementOrder,
@@ -133,8 +121,6 @@ export function assembleSolidHeatTransferMat(meshConfig, boundaryConditions) {
133121
// Determine the number of nodes in the reference element based on the first element in the nop array
134122
const numNodes = nop[0].length;
135123

136-
console.log(`Beginning matrix assembly for ${totalElements} elements...`);
137-
138124
// Matrix assembly
139125
for (let elementIndex = 0; elementIndex < totalElements; elementIndex++) {
140126
for (let localNodeIndex = 0; localNodeIndex < numNodes; localNodeIndex++) {
@@ -256,7 +242,7 @@ export function assembleSolidHeatTransferMat(meshConfig, boundaryConditions) {
256242
}
257243

258244
// Create an instance of ThermalBoundaryConditions
259-
console.log("Applying thermal boundary conditions...");
245+
debugLog("Applying thermal boundary conditions...");
260246
const thermalBoundaryConditions = new ThermalBoundaryConditions(
261247
boundaryConditions,
262248
boundaryElements,
@@ -277,13 +263,13 @@ export function assembleSolidHeatTransferMat(meshConfig, boundaryConditions) {
277263
convectionHeatTranfCoeff,
278264
convectionExtTemp
279265
);
280-
console.log("Convection boundary conditions applied");
266+
debugLog("Convection boundary conditions applied");
281267

282268
// Impose ConstantTemp boundary conditions
283269
thermalBoundaryConditions.imposeConstantTempBoundaryConditions(residualVector, jacobianMatrix);
284-
console.log("Constant temperature boundary conditions applied");
270+
debugLog("Constant temperature boundary conditions applied");
285271

286-
console.log("Solid heat transfer matrix assembly completed");
272+
basicLog("Solid heat transfer matrix assembly completed");
287273

288274
return {
289275
jacobianMatrix,

src/utilities/utilitiesScript.js

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,62 @@
88
// |_| | |_ //
99
// Website: https://feascript.com/ \__| //
1010

11+
// Global logging level
12+
let currentLogLevel = 'basic';
13+
14+
/**
15+
* Function to set the logging system level
16+
* @param {string} level - Logging level (basic, debug)
17+
*/
18+
export function logSystem(level) {
19+
if (level !== 'basic' && level !== 'debug') {
20+
console.log('%c[WARN] Invalid log level: ' + level + '. Using basic instead.', 'color: #FFC107; font-weight: bold;'); // Yellow for warnings
21+
currentLogLevel = 'basic';
22+
} else {
23+
currentLogLevel = level;
24+
basicLog(`Log level set to: ${level}`);
25+
}
26+
}
27+
28+
/**
29+
* Debug logging function - only logs if level is 'debug'
30+
* @param {string} message - Message to log
31+
*/
32+
export function debugLog(message) {
33+
if (currentLogLevel === 'debug') {
34+
console.log('%c[DEBUG] ' + message, 'color: #2196F3; font-weight: bold;'); // Blue color for debug
35+
}
36+
}
37+
38+
/**
39+
* Basic logging function - always logs
40+
* @param {string} message - Message to log
41+
*/
42+
export function basicLog(message) {
43+
console.log('%c[INFO] ' + message, 'color: #4CAF50; font-weight: bold;'); // Green color for basic info
44+
}
45+
46+
/**
47+
* Error logging function
48+
* @param {string} message - Message to log
49+
*/
50+
export function errorLog(message) {
51+
console.log('%c[ERROR] ' + message, 'color: #F44336; font-weight: bold;'); // Red color for errors
52+
}
53+
1154
/**
1255
* Function to handle version information and fetch the latest update date and release from GitHub
1356
*/
1457
export async function printVersion() {
15-
// log.info("Fetching latest FEAScript version information...");
16-
console.log("Fetching latest FEAScript version information...");
58+
basicLog("Fetching latest FEAScript version information...");
1759
try {
18-
// Fetch the latest commit date
1960
const commitResponse = await fetch("https://api.github.com/repos/FEAScript/FEAScript/commits/main");
2061
const commitData = await commitResponse.json();
2162
const latestCommitDate = new Date(commitData.commit.committer.date).toLocaleString();
22-
// log.info(`Latest FEAScript update: ${latestCommitDate}`);
23-
console.log(`Latest FEAScript update: ${latestCommitDate}`);
63+
basicLog(`Latest FEAScript update: ${latestCommitDate}`);
2464
return latestCommitDate;
2565
} catch (error) {
26-
// log.error("Failed to fetch version information:", error);
27-
console.error("Failed to fetch version information:", error);
66+
errorLog("Failed to fetch version information: " + error);
2867
return "Version information unavailable";
2968
}
3069
}
31-
32-
/**
33-
* Set the logging level for all loggers
34-
* @param {string} level - The log level to set (trace, debug, info, warn, error)
35-
*/
36-
export function setLogLevel(level) {
37-
// Logging is temporarily disabled
38-
// loggers.setAllLogLevels(level);
39-
// log.info(`Log level set to: ${level}`);
40-
console.log(`Log level set to: ${level} (logging temporarily disabled)`);
41-
}

third-party/loglevel.min.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)