This commit is contained in:
2026-08-29 18:47:45 +07:00
parent c28b7ebf84
commit 3780eb41df
2 changed files with 60 additions and 2 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
name = "GeneralUtils" name = "GeneralUtils"
uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe" uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
version = "0.6.2" version = "0.6.3"
authors = ["tonaerospace <tonaerospace.etc@gmail.com>"] authors = ["tonaerospace <tonaerospace.etc@gmail.com>"]
[deps] [deps]
+59 -1
View File
@@ -4,7 +4,7 @@ export timedifference, showstracktrace, findHighestIndexKey, uuid4snakecase, rep
findMatchingDictKey, randstring, randstrings, timeout, findMatchingDictKey, randstring, randstrings, timeout,
dataframeToCSV, dfToVectorDict, disintegrate_vectorDict, getDataFrameValue, dfRowtoString, dataframeToCSV, dfToVectorDict, disintegrate_vectorDict, getDataFrameValue, dfRowtoString,
dfToString, dataframe_to_json_list, dictToString, dictToString_noKey, issomething, 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, countGivenWords, remove_french_accents, removestring,
extractTextBetweenCharacter, extractTextBetweenString, extractTextBetweenCharacter, extractTextBetweenString,
convertCamelSnakeKebabCase, fitrange, recentElementsIndex, nonRecentElementsIndex convertCamelSnakeKebabCase, fitrange, recentElementsIndex, nonRecentElementsIndex
@@ -1173,6 +1173,64 @@ function removestring(text::String, removelist::Vector{String})::String
end 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