From 3780eb41dfcf289e1b891cbea642bc5b99045680 Mon Sep 17 00:00:00 2001 From: narawat Date: Sat, 29 Aug 2026 18:47:45 +0700 Subject: [PATCH] update --- Project.toml | 2 +- src/util.jl | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index d798a3c..13dd9d5 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "GeneralUtils" uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe" -version = "0.6.2" +version = "0.6.3" authors = ["tonaerospace "] [deps] diff --git a/src/util.jl b/src/util.jl index 5159d3f..cc02996 100644 --- a/src/util.jl +++ b/src/util.jl @@ -4,7 +4,7 @@ export timedifference, showstracktrace, findHighestIndexKey, uuid4snakecase, rep findMatchingDictKey, randstring, randstrings, timeout, dataframeToCSV, dfToVectorDict, disintegrate_vectorDict, getDataFrameValue, dfRowtoString, dfToString, dataframe_to_json_list, dictToString, dictToString_noKey, issomething, - dictToString_numbering, extract_triple_backtick_text, + dictToString_numbering, extract_triple_backtick_text, logger, countGivenWords, remove_french_accents, removestring, extractTextBetweenCharacter, extractTextBetweenString, convertCamelSnakeKebabCase, fitrange, recentElementsIndex, nonRecentElementsIndex @@ -1173,6 +1173,64 @@ function removestring(text::String, removelist::Vector{String})::String end +""" + log(msg; service_name, nats_conn, nats_status_topic, log_dir, log_file) + +Write a log entry to a local markdown file and optionally publish it to a NATS status topic. +Each entry is prefixed with a timestamp and service name: `-[YYYY-MM-DDTHH:MM:SS.sss] [serviceName] msg`. +The log file is capped at 100 entries (oldest entries are truncated on rotation). + +# Arguments +- `msg::String`: Log message body. +- `service_name::String`: Service identifier, inserted as `[name]` prefix. Defaults to `config["thisServiceName"]`. +- `nats_conn`: Optional NATS connection handle. Pass to enable NATS publishing. +- `nats_status_topic`: Optional NATS topic to publish log entries to. +- `log_dir::String`: Directory for the log file. Defaults to `"./log"`. +- `log_file::String`: Full path to the markdown log file. Defaults to `"./log/log.md"`. +- `max_log_entries::Int`: Maximum number of log entries to keep in the file before rotating. Defaults to `100`. +""" +function logger(msg::String; service_name::String=config["thisServiceName"], + nats_conn=nothing, nats_status_topic=nothing, log_dir::String="./log", + log_file::String=joinpath(log_dir, "log.md"), max_log_entries::Int=100)::Nothing + + timestamp = Dates.format(now(), "yyyy-mm-ddTHH:MM:SS.sss") + svc_prefix = isempty(service_name) ? "" : "[$service_name] " + entry = "- [$timestamp] $svc_prefix$msg\n" + mkpath(log_dir) + lk = ReentrantLock() + lock(lk) do + if isfile(log_file) + content = read(log_file, String) + entries = split(content, r"- \[", keepempty=false) + if length(entries) >= max_log_entries + open(log_file, "w") do f + write(f, "- [") + for e in entries[2:end] + write(f, "- [", e) + end + write(f, entry) + end + else + open(log_file, "a") do f + write(f, entry) + end + end + else + open(log_file, "w") do f + write(f, entry) + end + end + end + if nats_conn !== nothing && nats_status_topic !== nothing + try + NATS.publish(nats_conn, nats_status_topic, entry) + catch e + @error "log NATS publish failed" exception=e + end + end + return nothing +end +