Hello, blightmud-batmud-logger
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"Lua.diagnostics.globals": ["cformat", "mud"]
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
FROM alpine:3.19
|
||||
|
||||
# zstd for compression, tzdata so cron fires at correct local time
|
||||
RUN apk add --no-cache zstd tzdata
|
||||
|
||||
COPY compress.sh /usr/local/bin/compress.sh
|
||||
RUN chmod +x /usr/local/bin/compress.sh
|
||||
|
||||
# Install crontab: run compress at 02:00 every night
|
||||
RUN echo "0 2 * * * /usr/local/bin/compress.sh > /proc/1/fd/1 2>&1" \
|
||||
| crontab -
|
||||
|
||||
# crond -f: foreground (required for containers)
|
||||
# crond -l 2: log level warning and above
|
||||
CMD ["crond", "-f", "-l", "2"]
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env sh
|
||||
# Compress all .log files not modified today across all character subdirs.
|
||||
# Expects /logs to be mounted from the host.
|
||||
|
||||
LOG_DIR="${LOG_DIR:-/logs}"
|
||||
TODAY=$(date +%Y-%m-%d)
|
||||
|
||||
find "$LOG_DIR" -maxdepth 2 -name "*.log" | while read -r f; do
|
||||
FILE_DATE=$(date -r "$f" +%Y-%m-%d 2>/dev/null)
|
||||
|
||||
if [ "$FILE_DATE" = "$TODAY" ]; then
|
||||
echo "[$(date +%H:%M:%S)] Skipping active log: $f"
|
||||
continue
|
||||
fi
|
||||
|
||||
echo "[$(date +%H:%M:%S)] Compressing: $f"
|
||||
if zstd -19 --rm -q "$f"; then
|
||||
echo "[$(date +%H:%M:%S)] Done: ${f}.zst"
|
||||
else
|
||||
echo "[$(date +%H:%M:%S)] ERROR compressing: $f" >&2
|
||||
fi
|
||||
done
|
||||
|
||||
echo "[$(date +%H:%M:%S)] Compression run complete."
|
||||
@@ -0,0 +1,44 @@
|
||||
OUTPUT = ":"
|
||||
INPUT = ">"
|
||||
|
||||
local current_log_filepath = nil
|
||||
local current_log_file = nil
|
||||
|
||||
local get_current_log_filepath = function()
|
||||
local date = os.date("%Y-%m-%d")
|
||||
local data_home = os.getenv("XDG_DATA_HOME") or string.format("%s/.local/share", os.getenv("HOME"))
|
||||
return string.format("%s/blightmud/logs/batmud/%s/%s.log", data_home, os.getenv("BATMUD_CHAR"), date)
|
||||
end
|
||||
|
||||
local write_log = function(line, type)
|
||||
local path = get_current_log_filepath()
|
||||
if current_log_filepath == nil or current_log_filepath ~= path then
|
||||
current_log_filepath = path
|
||||
os.execute(string.format("mkdir -p %q", path:match("^(.*)/[^/]+$")))
|
||||
current_log_file = assert(io.open(path, "a"))
|
||||
end
|
||||
|
||||
local log_line = cformat("<black>[%s]%s<reset> %s\n", os.date("%H:%M:%S"), type, line)
|
||||
if current_log_file ~= nil then
|
||||
assert(current_log_file:write(log_line))
|
||||
assert(current_log_file:flush())
|
||||
end
|
||||
end
|
||||
|
||||
mud.add_output_listener(function(line)
|
||||
if not line:skip_log() then
|
||||
if line:replacement() == nil then
|
||||
write_log(line:raw(), OUTPUT)
|
||||
else
|
||||
write_log(line:replacement(), OUTPUT)
|
||||
end
|
||||
end
|
||||
return line
|
||||
end)
|
||||
|
||||
mud.add_input_listener(function(line)
|
||||
if not line:skip_log() then
|
||||
write_log(line:raw(), INPUT)
|
||||
end
|
||||
return line
|
||||
end)
|
||||
Reference in New Issue
Block a user