project migration to typescript

Project migration to typescript
This commit is contained in:
Dariusz L
2025-07-04 04:22:51 +02:00
parent 3e4cdf10bc
commit 5adc77471f
60 changed files with 12565 additions and 3021 deletions

View File

@@ -8,6 +8,20 @@
* - Możliwość zapisywania logów do localStorage
* - Możliwość eksportu logów
*/
function padStart(str, targetLength, padString) {
targetLength = targetLength >> 0;
padString = String(padString || ' ');
if (str.length > targetLength) {
return String(str);
}
else {
targetLength = targetLength - str.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength / padString.length);
}
return padString.slice(0, targetLength) + String(str);
}
}
export const LogLevel = {
DEBUG: 0,
INFO: 1,
@@ -36,25 +50,22 @@ const LEVEL_NAMES = {
[LogLevel.WARN]: 'WARN',
[LogLevel.ERROR]: 'ERROR',
};
class Logger {
constructor() {
this.config = {...DEFAULT_CONFIG};
this.config = { ...DEFAULT_CONFIG };
this.logs = [];
this.enabled = true;
this.loadConfig();
}
/**
* Konfiguracja loggera
* @param {Object} config - Obiekt konfiguracyjny
* @param {Partial<LoggerConfig>} config - Obiekt konfiguracyjny
*/
configure(config) {
this.config = {...this.config, ...config};
this.config = { ...this.config, ...config };
this.saveConfig();
return this;
}
/**
* Włącz/wyłącz logger globalnie
* @param {boolean} enabled - Czy logger ma być włączony
@@ -63,42 +74,39 @@ class Logger {
this.enabled = enabled;
return this;
}
/**
* Ustaw globalny poziom logowania
* @param {LogLevel} level - Poziom logowania
* @param {LogLevels} level - Poziom logowania
*/
setGlobalLevel(level) {
this.config.globalLevel = level;
this.saveConfig();
return this;
}
/**
* Ustaw poziom logowania dla konkretnego modułu
* @param {string} module - Nazwa modułu
* @param {LogLevel} level - Poziom logowania
* @param {LogLevels} level - Poziom logowania
*/
setModuleLevel(module, level) {
this.config.moduleSettings[module] = level;
this.saveConfig();
return this;
}
/**
* Sprawdź, czy dany poziom logowania jest aktywny dla modułu
* @param {string} module - Nazwa modułu
* @param {LogLevel} level - Poziom logowania do sprawdzenia
* @param {LogLevels} level - Poziom logowania do sprawdzenia
* @returns {boolean} - Czy poziom jest aktywny
*/
isLevelEnabled(module, level) {
if (!this.enabled) return false;
if (!this.enabled)
return false;
if (this.config.moduleSettings[module] !== undefined) {
return level >= this.config.moduleSettings[module];
}
return level >= this.config.globalLevel;
}
/**
* Formatuj znacznik czasu
* @returns {string} - Sformatowany znacznik czasu
@@ -107,21 +115,20 @@ class Logger {
const now = new Date();
const format = this.config.timestampFormat;
return format
.replace('HH', String(now.getHours()).padStart(2, '0'))
.replace('mm', String(now.getMinutes()).padStart(2, '0'))
.replace('ss', String(now.getSeconds()).padStart(2, '0'))
.replace('SSS', String(now.getMilliseconds()).padStart(3, '0'));
.replace('HH', padStart(String(now.getHours()), 2, '0'))
.replace('mm', padStart(String(now.getMinutes()), 2, '0'))
.replace('ss', padStart(String(now.getSeconds()), 2, '0'))
.replace('SSS', padStart(String(now.getMilliseconds()), 3, '0'));
}
/**
* Zapisz log
* @param {string} module - Nazwa modułu
* @param {LogLevel} level - Poziom logowania
* @param {Array} args - Argumenty do zalogowania
* @param {LogLevels} level - Poziom logowania
* @param {any[]} args - Argumenty do zalogowania
*/
log(module, level, ...args) {
if (!this.isLevelEnabled(module, level)) return;
if (!this.isLevelEnabled(module, level))
return;
const timestamp = this.formatTimestamp();
const levelName = LEVEL_NAMES[level];
const logData = {
@@ -141,13 +148,12 @@ class Logger {
}
this.printToConsole(logData);
}
/**
* Wyświetl log w konsoli
* @param {Object} logData - Dane logu
* @param {LogData} logData - Dane logu
*/
printToConsole(logData) {
const {timestamp, module, level, levelName, args} = logData;
const { timestamp, module, level, levelName, args } = logData;
const prefix = `[${timestamp}] [${module}] [${levelName}]`;
if (this.config.useColors && typeof console.log === 'function') {
const color = COLORS[level] || '#000000';
@@ -156,36 +162,35 @@ class Logger {
}
console.log(prefix, ...args);
}
/**
* Zapisz logi do localStorage
*/
saveLogs() {
if (typeof localStorage !== 'undefined' && this.config.saveToStorage) {
try {
const simplifiedLogs = this.logs.map(log => ({
const simplifiedLogs = this.logs.map((log) => ({
t: log.timestamp,
m: log.module,
l: log.level,
a: log.args.map(arg => {
a: log.args.map((arg) => {
if (typeof arg === 'object') {
try {
return JSON.stringify(arg);
} catch (e) {
}
catch (e) {
return String(arg);
}
}
return arg;
})
}));
localStorage.setItem(this.config.storageKey, JSON.stringify(simplifiedLogs));
} catch (e) {
}
catch (e) {
console.error('Failed to save logs to localStorage:', e);
}
}
}
/**
* Załaduj logi z localStorage
*/
@@ -196,12 +201,12 @@ class Logger {
if (storedLogs) {
this.logs = JSON.parse(storedLogs);
}
} catch (e) {
}
catch (e) {
console.error('Failed to load logs from localStorage:', e);
}
}
}
/**
* Zapisz konfigurację do localStorage
*/
@@ -209,12 +214,12 @@ class Logger {
if (typeof localStorage !== 'undefined') {
try {
localStorage.setItem('layerforge_logger_config', JSON.stringify(this.config));
} catch (e) {
}
catch (e) {
console.error('Failed to save logger config to localStorage:', e);
}
}
}
/**
* Załaduj konfigurację z localStorage
*/
@@ -223,14 +228,14 @@ class Logger {
try {
const storedConfig = localStorage.getItem('layerforge_logger_config');
if (storedConfig) {
this.config = {...this.config, ...JSON.parse(storedConfig)};
this.config = { ...this.config, ...JSON.parse(storedConfig) };
}
} catch (e) {
}
catch (e) {
console.error('Failed to load logger config from localStorage:', e);
}
}
}
/**
* Wyczyść wszystkie logi
*/
@@ -241,33 +246,29 @@ class Logger {
}
return this;
}
/**
* Eksportuj logi do pliku
* @param {string} format - Format eksportu ('json' lub 'txt')
* @param {'json' | 'txt'} format - Format eksportu
*/
exportLogs(format = 'json') {
if (this.logs.length === 0) {
console.warn('No logs to export');
return;
}
let content;
let mimeType;
let extension;
if (format === 'json') {
content = JSON.stringify(this.logs, null, 2);
mimeType = 'application/json';
extension = 'json';
} else {
content = this.logs.map(log =>
`[${log.timestamp}] [${log.module}] [${log.levelName}] ${log.args.join(' ')}`
).join('\n');
}
else {
content = this.logs.map((log) => `[${log.timestamp}] [${log.module}] [${log.levelName}] ${log.args.join(' ')}`).join('\n');
mimeType = 'text/plain';
extension = 'txt';
}
const blob = new Blob([content], {type: mimeType});
const blob = new Blob([content], { type: mimeType });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
@@ -277,44 +278,39 @@ class Logger {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
/**
* Log na poziomie DEBUG
* @param {string} module - Nazwa modułu
* @param {...any} args - Argumenty do zalogowania
* @param {any[]} args - Argumenty do zalogowania
*/
debug(module, ...args) {
this.log(module, LogLevel.DEBUG, ...args);
}
/**
* Log na poziomie INFO
* @param {string} module - Nazwa modułu
* @param {...any} args - Argumenty do zalogowania
* @param {any[]} args - Argumenty do zalogowania
*/
info(module, ...args) {
this.log(module, LogLevel.INFO, ...args);
}
/**
* Log na poziomie WARN
* @param {string} module - Nazwa modułu
* @param {...any} args - Argumenty do zalogowania
* @param {any[]} args - Argumenty do zalogowania
*/
warn(module, ...args) {
this.log(module, LogLevel.WARN, ...args);
}
/**
* Log na poziomie ERROR
* @param {string} module - Nazwa modułu
* @param {...any} args - Argumenty do zalogowania
* @param {any[]} args - Argumenty do zalogowania
*/
error(module, ...args) {
this.log(module, LogLevel.ERROR, ...args);
}
}
export const logger = new Logger();
export const debug = (module, ...args) => logger.debug(module, ...args);
export const info = (module, ...args) => logger.info(module, ...args);
@@ -323,5 +319,4 @@ export const error = (module, ...args) => logger.error(module, ...args);
if (typeof window !== 'undefined') {
window.LayerForgeLogger = logger;
}
export default logger;
export default logger;