Skip to content

Commit

Permalink
Adds timestamps to the log format
Browse files Browse the repository at this point in the history
  • Loading branch information
SirMoM committed Oct 9, 2020
1 parent 0fc607e commit 85fc770
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,44 @@ singleton:

Read the code for details about the API, it's extensively documented.

## Timestamp and log format
All logging levels include an timestamp, which will be formated acording to the date_time template in the Module.

Also the log format can be changed based on the keywords in the `FORMAT_IDS` dictionary.

### Formatting fields
**Timestamp fields**:
* yyyy = Year
* mm = Month
* dd = Day
* hh = Hour
* MM = Minutes
* ss = Seconds

**Log format fields**:
* {LVL} = Level of the log
* {MOD} = Module that does the logging
* {MSG} = Message from the user
* {TIME} = Timestamp when the logging occurred

### Example:
```gdscript
Logger.get_module("main").time_template = "dd.mm.yyyy hh:MM:ss"
Logger.error(msg)
Logger.get_module("main").time_template = "hh:MM:ss"
Logger.error(msg)
Logger.output_format = "[{LVL}] {MSG} at {TIME}"
Logger.error(msg)
```
Results in: \
![Different log formats](/img/log_formating.png)





Licensing
---------

Expand Down
Binary file added img/log_formating.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 29 additions & 5 deletions logger.gd
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class Module:
var output_level = 0
var output_strategies = []
var logfile = null
var time_template = "hh:MM:ss"

func _init(_name, _output_level, _output_strategies, _logfile):
name = _name
Expand Down Expand Up @@ -218,9 +219,10 @@ const MAX_STRATEGY = STRATEGY_MEMORY*2 - 1

# Output format identifiers
const FORMAT_IDS = {
"level": "{LVL}",
"module": "{MOD}",
"message": "{MSG}"
"level": "{LVL}",
"module": "{MOD}",
"message": "{MSG}",
"timestamp": "{TIME}"
}

# Queue modes
Expand All @@ -243,7 +245,7 @@ var default_logfile_path = "user://%s.log" % ProjectSettings.get_setting("applic
var default_configfile_path = "user://%s.cfg" % PLUGIN_NAME

# e.g. "[INFO] [main] The young alpaca started growing a goatie."
var output_format = "[{LVL}] [{MOD}] {MSG}"
var output_format = "[{TIME}] [{LVL}] [{MOD}] {MSG}"

# Holds the name of the debug module for easy usage across all logging functions.
var default_module_name = "main"
Expand Down Expand Up @@ -441,11 +443,33 @@ func get_default_output_level():
# Output formatting
# -----------------

static func format(template, level, module, message):
# Formattes the fields:
# * yyyy = Year
# * mm = Month
# * dd = Day
# * hh = Hour
# * MM = Minutes
# * ss = Seconds
static func formatted_datetime(template: String):
var datetime = OS.get_datetime()

var result = template
result = template.replacen("yyyy", "%04d" % [datetime.year])
result = result.replacen("mm", "%02d" % [datetime.month])
result = result.replacen("dd", "%02d" % [datetime.day])
result = result.replacen("hh", "%02d" % [datetime.hour])
result = result.replacen("MM", "%02d" % [datetime.minute])
result = result.replacen("ss", "%02d" % [datetime.second])

return result


func format(template, level, module, message):
var output = template
output = output.replace(FORMAT_IDS.level, LEVELS[level])
output = output.replace(FORMAT_IDS.module, module)
output = output.replace(FORMAT_IDS.message, message)
output = output.replace(FORMAT_IDS.timestamp, formatted_datetime(get_module(module).time_template))
return output

func set_output_format(new_format):
Expand Down

0 comments on commit 85fc770

Please sign in to comment.