{"id":21126014,"url":"https://github.com/julialogging/loggingformats.jl","last_synced_at":"2025-03-14T11:42:10.887Z","repository":{"id":45362866,"uuid":"405903454","full_name":"JuliaLogging/LoggingFormats.jl","owner":"JuliaLogging","description":"Logger output formats for Julia.","archived":false,"fork":false,"pushed_at":"2024-05-30T15:58:35.000Z","size":25,"stargazers_count":6,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-20T21:22:51.922Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Julia","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JuliaLogging.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-09-13T09:05:02.000Z","updated_at":"2025-01-05T17:14:08.000Z","dependencies_parsed_at":"2024-04-09T23:47:38.173Z","dependency_job_id":"4452d114-c338-44af-b1eb-1ee7343ddfe1","html_url":"https://github.com/JuliaLogging/LoggingFormats.jl","commit_stats":{"total_commits":15,"total_committers":4,"mean_commits":3.75,"dds":0.2666666666666667,"last_synced_commit":"2b552738490a728f28d94fc4a2098f1fae4cb296"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuliaLogging%2FLoggingFormats.jl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuliaLogging%2FLoggingFormats.jl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuliaLogging%2FLoggingFormats.jl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuliaLogging%2FLoggingFormats.jl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JuliaLogging","download_url":"https://codeload.github.com/JuliaLogging/LoggingFormats.jl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243573168,"owners_count":20312879,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T04:38:46.433Z","updated_at":"2025-03-14T11:42:10.865Z","avatar_url":"https://github.com/JuliaLogging.png","language":"Julia","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LoggingFormats.jl\n\nThis package is an aggregation of various useful format functions to use with the\n[FormatLogger](https://github.com/JuliaLogging/LoggingExtras.jl#formatlogger-sink) from the\n[LoggingExtras](https://github.com/JuliaLogging/LoggingExtras.jl) package.\n\nCurrently, the following functors are available:\n- `JSON`: output log events as JSON\n- `LogFmt`: output log events formatted as [logfmt](https://brandur.org/logfmt)\n- `Truncated`: truncation of log messages\n\n## `JSON`: Output log events as JSON\n\n`LoggingFormats.JSON()` is a function which formats the log message and the log metadata as JSON.\nExample:\n\n```julia\njulia\u003e using LoggingFormats, LoggingExtras\n\njulia\u003e with_logger(FormatLogger(LoggingFormats.JSON(), stderr)) do\n           @info \"hello, world\"\n           @error \"something is wrong\"\n       end\n{\"level\":\"info\",\"msg\":\"hello, world\",\"module\":\"Main\",\"file\":\"REPL[10]\",\"line\":2,\"group\":\"REPL[10]\",\"id\":\"Main_6972c828\",\"kwargs\":{}}\n{\"level\":\"error\",\"msg\":\"something is wrong\",\"module\":\"Main\",\"file\":\"REPL[10]\",\"line\":3,\"group\":\"REPL[10]\",\"id\":\"Main_2289c7f9\",\"kwargs\":{}}\n```\n\nOne can also pass `recursive=true` to recursively serialize the `kwargs` as JSON:\n\n```julia\njulia\u003e using LoggingFormats, LoggingExtras\n\njulia\u003e with_logger(FormatLogger(LoggingFormats.JSON(; recursive=true), stderr)) do\n                  @info \"hello, world\" key=Dict(\"hello\" =\u003e true)\n       end\n{\"level\":\"info\",\"msg\":\"hello, world\",\"module\":\"Main\",\"file\":\"REPL[18]\",\"line\":2,\"group\":\"REPL[18]\",\"id\":\"Main_ffce16b5\",\"kwargs\":{\"key\":{\"hello\":true}}}\n```\n\nIf it encounters something which does not have a defined `StructTypes.StructType` to use\nfor serializing to JSON (or otherwise errors when serializing to JSON), it will fallback to converting the objects to strings, like the default `recursive=false` option does. Handles exceptions specially, by printing errors and stacktraces using `Base.showerror`.\n\n```julia\njulia\u003e f() = try\n                throw(ArgumentError(\"Bad input\"))\n            catch e\n                @error \"Input error\" exception=(e, catch_backtrace())\n            end\n\njulia\u003e with_logger(f, FormatLogger(LoggingFormats.JSON(; recursive=true), stderr))\n{\"level\":\"error\",\"msg\":\"Input error\",\"module\":\"Main\",\"file\":\"REPL[2]\",\"line\":4,\"group\":\"REPL[2]\",\"id\":\"Main_a226875f\",\"kwargs\":{\"exception\":\"ERROR: ArgumentError: Bad input\\nStacktrace:\\n [1] f()\\n   @ Main ./REPL[2]:2\\n [2] with_logstate(f::Function, logstate::Any)\\n   @ Base.CoreLogging ./logging.jl:511\\n [3] with_logger(f::Function, logger::FormatLogger)\\n   @ Base.CoreLogging ./logging.jl:623\\n [4] top-level scope\\n   @ REPL[3]:1\\n\"}}\n```\n\n## `LogFmt`: Format log events as logfmt\n\n`LoggingFormats.LogFmt()` is a function which formats the log message in the\n[logfmt](https://brandur.org/logfmt) format. Example:\n\n```julia\njulia\u003e using LoggingFormats, LoggingExtras\n\njulia\u003e with_logger(FormatLogger(LoggingFormats.LogFmt(), stderr)) do\n           @info \"hello, world\"\n           @error \"something is wrong\"\n       end\nlevel=info msg=\"hello, world\" module=Main file=\"REPL[2]\" line=2 group=\"REPL[2]\" id=Main_6972c827\nlevel=error msg=\"something is wrong\" module=Main file=\"REPL[2]\" line=3 group=\"REPL[2]\" id=Main_2289c7f8\n```\n\nSimilarly to the JSON logger, `LogFmt` handles exceptions specially, by printing errors and stacktraces using `Base.showerror`.\n\nOne can also restrict the \"standard\" keys used in the log message, for example:\n\n```julia\njulia\u003e using LoggingFormats, LoggingExtras\n\njulia\u003e with_logger(FormatLogger(LoggingFormats.LogFmt((:level, :message, :file)), stderr)) do\n           @info \"hello, world\" extra=\"bye\"\n           @error \"something is wrong\"\n       end\nlevel=info msg=\"hello, world\" file=\"REPL[5]\" extra=\"bye\"\nlevel=error msg=\"something is wrong\" file=\"REPL[5]\"\n```\n\nNote here that the `module`, `group`, `id` do not appear, since they weren't specified, but the \"custom\" key `extra` still appears. The full set of standard keys is `(:level, :msg, :module, :file, :line, :group, :id)`, which are all used by default, in that order.\n\n## `Truncated`: Truncate long variables and messages\n\n`LoggingFormats.Truncated(max_var_len=5_000)` is a function which formats data in similar manner as `ConsoleLogger`,\nbut with truncation of string representation when it exceeds `max_var_len`.\nThis format truncates the length of message itself, and truncates string representation of\nindividual variables, but does not truncate the size of whole printed text.\n\nSee the examples:\n\n```julia\njulia\u003e using LoggingFormats, LoggingExtras\n\njulia\u003e with_logger(FormatLogger(LoggingFormats.Truncated(30))) do\n    short_var = \"a\"^5\n    long_var = \"a\"^50\n    @info \"a short message\" short_var long_var\n    @info \"a very long message \"^20 short_var long_var\nend\n┌ Info: a short message\n│   short_var = aaaaa\n│   long_var = aaaaaaaaaaaa…\n└ @ Main REPL[46]:4\n┌ Info: a very long message a very lo…\n│   short_var = aaaaa\n│   long_var = aaaaaaaaaaaa…\n└ @ Main REPL[46]:5\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjulialogging%2Floggingformats.jl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjulialogging%2Floggingformats.jl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjulialogging%2Floggingformats.jl/lists"}