The Logging module provides comprehensive logging functionality for the Game Macro system, including multiple log sinks and flexible configuration.
| English Version | 中文版本 |
The Logging module provides:
The logging system is designed with a flexible sink architecture:
Main logging management and coordination system.
Multiple log sink implementations for different destinations.
File-based logging with rotation and compression.
In-memory logging for real-time monitoring.
Pipe-based logging for external log processors.
Configurable logging settings and behavior.
LogConfig := {
Level: "INFO",
Format: "{timestamp} [{level}] {module}: {message}",
Sinks: ["File", "Memory"],
FileConfig: {
Path: "logs/app.log",
MaxSize: 10485760, ; 10MB
MaxFiles: 5,
Compress: true
},
MemoryConfig: {
MaxEntries: 1000,
AutoFlush: true
}
}
Standardized log entry format.
LogEntry := {
Timestamp: "2024-01-01T12:00:00.000Z",
Level: "INFO",
Module: "RotationEngine",
Message: "Rotation started successfully",
Data: {},
ThreadId: 1
}
Flexible sink architecture for different logging destinations.
Sink := {
Type: "File",
IsEnabled: true,
Level: "INFO",
Write: Func("WriteLogEntry"),
Flush: Func("FlushSink"),
Close: Func("CloseSink")
}
Initializes the logging system.
Parameters:
config (Map): Logging configurationReturns: Boolean indicating success
Shuts down the logging system.
Parameters: None
Returns: Boolean indicating success
Sets the global log level.
Parameters:
level (String): Log level (DEBUG, INFO, WARNING, ERROR)Returns: Boolean indicating success
Gets the current log level.
Parameters: None
Returns: Current log level
Logs a debug message.
Parameters:
module (String): Module namemessage (String): Log messagedata (Map): Additional data (optional)Returns: Boolean indicating success
Logs an info message.
Parameters:
module (String): Module namemessage (String): Log messagedata (Map): Additional data (optional)Returns: Boolean indicating success
Logs a warning message.
Parameters:
module (String): Module namemessage (String): Log messagedata (Map): Additional data (optional)Returns: Boolean indicating success
Logs an error message.
Parameters:
module (String): Module namemessage (String): Log messagedata (Map): Additional data (optional)Returns: Boolean indicating success
Adds a new log sink.
Parameters:
sinkConfig (Map): Sink configurationReturns: Sink ID
Removes a log sink.
Parameters:
sinkId (String): Sink identifierReturns: Boolean indicating success
Enables a log sink.
Parameters:
sinkId (String): Sink identifierReturns: Boolean indicating success
Disables a log sink.
Parameters:
sinkId (String): Sink identifierReturns: Boolean indicating success
Gets log entries with optional filtering.
Parameters:
filter (Map): Filter criteria (optional)Returns: Array of log entries
Clears all log entries.
Parameters: None
Returns: Boolean indicating success
Flushes all log sinks.
Parameters: None
Returns: Boolean indicating success
; Initialize logging system
logConfig := {
Level: "INFO",
Format: "{timestamp} [{level}] {module}: {message}",
Sinks: ["File", "Memory"]
}
if (!Logger_Init(logConfig)) {
MsgBox("Failed to initialize logging system")
ExitApp(1)
}
; Basic logging
Logger_Info("Main", "Application started successfully")
Logger_Debug("Config", "Configuration loaded", App["Config"])
; Advanced logging configuration
advancedConfig := {
Level: "DEBUG",
Format: "{timestamp} [{level:5}] {module:15} {message}",
Sinks: ["File", "Memory", "Pipe"],
FileConfig: {
Path: "logs/game-macro.log",
MaxSize: 10485760, ; 10MB
MaxFiles: 10,
Compress: true
},
MemoryConfig: {
MaxEntries: 2000,
AutoFlush: false
},
PipeConfig: {
PipeName: "GameMacroLogs",
BufferSize: 4096
}
}
Logger_Init(advancedConfig)
; Rotation engine logging
RotationEngine_Start() {
Logger_Info("RotationEngine", "Starting rotation engine")
; Engine initialization
if (!Engine_Init()) {
Logger_Error("RotationEngine", "Failed to initialize engine")
return false
}
Logger_Debug("RotationEngine", "Engine initialized successfully")
return true
}
; Rule engine logging with data
RuleEngine_AddRule(rule) {
Logger_Info("RuleEngine", "Adding new rule", Map(
"ruleId", rule["Id"],
"conditionType", rule["Condition"]["Type"]
))
; Rule validation and addition
if (ValidateRule(rule)) {
Rules[rule["Id"]] := rule
Logger_Debug("RuleEngine", "Rule added successfully")
} else {
Logger_Warning("RuleEngine", "Invalid rule rejected")
}
}
; Comprehensive error handling
HandleSkillExecution(skillId) {
try {
Logger_Debug("CastEngine", "Executing skill", Map("skillId", skillId))
if (!CastEngine_IsSkillReady(skillId)) {
Logger_Warning("CastEngine", "Skill not ready", Map("skillId", skillId))
return false
}
result := CastEngine_ExecuteSkill(skillId)
if (result) {
Logger_Info("CastEngine", "Skill executed successfully", Map("skillId", skillId))
} else {
Logger_Error("CastEngine", "Skill execution failed", Map("skillId", skillId))
}
return result
} catch e {
Logger_Error("CastEngine", "Skill execution error", Map(
"skillId", skillId,
"error", e.Message,
"stack", e.Stack
))
return false
}
}
; Log monitoring function
MonitorLogs() {
; Get recent error logs
errorLogs := Logger_GetLogEntries({
Level: "ERROR",
Since: Utils_GetTimestamp() - 300000 ; Last 5 minutes
})
if (errorLogs.Length > 0) {
Logger_Warning("Monitor", "Errors detected in recent logs", Map("count", errorLogs.Length))
; Send alert or take action
for log in errorLogs {
UI_ShowAlert("Error detected: " . log["Message"])
}
}
; Check log file size
fileInfo := Logger_GetFileInfo()
if (fileInfo["Size"] > fileInfo["MaxSize"] * 0.8) {
Logger_Info("Monitor", "Log file approaching size limit")
}
}
; Log cleanup
CleanupOldLogs() {
; Clear logs older than 7 days
cutoff := Utils_GetTimestamp() - 7 * 24 * 60 * 60 * 1000
oldLogs := Logger_GetLogEntries({"Before": cutoff})
if (oldLogs.Length > 0) {
Logger_Info("Cleanup", "Cleaning up old logs", Map("count", oldLogs.Length))
Logger_ClearLogs({"Before": cutoff})
}
}
Logging settings are configured in the main application:
App["LoggingConfig"] := {
Level: "INFO",
Format: "{timestamp} [{level}] {module}: {message}",
Sinks: ["File", "Memory"],
File: {
Path: "logs/app.log",
MaxSize: 10485760,
MaxFiles: 5,
Compress: true
},
Memory: {
MaxEntries: 1000
},
Performance: {
EnableMetrics: true,
SampleInterval: 60000
}
}
Individual modules can have specific logging configurations:
; Module-specific logging levels
App["ModuleLogLevels"] := {
"RotationEngine": "DEBUG",
"CastEngine": "INFO",
"BuffEngine": "WARNING",
"UI": "ERROR"
}
The logging module includes comprehensive error handling:
The module provides debugging capabilities:
; Enable logging debugging
Logger_EnableDebug()
; Get debug information
debugInfo := Logger_GetDebugInfo()
; Test logging functionality
Logger_TestSinks()
Built-in tools for log analysis:
; Analyze log patterns
patterns := Logger_AnalyzePatterns({
TimeRange: "last-hour",
Module: "RotationEngine"
})
; Generate log reports
report := Logger_GenerateReport({
Period: "daily",
Format: "HTML"
})