{"id":20040382,"url":"https://github.com/caddyserver/transform-encoder","last_synced_at":"2025-04-12T23:38:12.366Z","repository":{"id":38440034,"uuid":"267697823","full_name":"caddyserver/transform-encoder","owner":"caddyserver","description":"Log encoder module for custom log formats","archived":false,"fork":false,"pushed_at":"2025-03-13T00:22:55.000Z","size":73,"stargazers_count":90,"open_issues_count":2,"forks_count":21,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-12T23:38:07.956Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/caddyserver.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":"2020-05-28T21:12:38.000Z","updated_at":"2025-04-11T04:41:31.000Z","dependencies_parsed_at":"2025-04-04T04:01:46.053Z","dependency_job_id":"b950ec42-f290-44cc-8bba-a0cdf864d87d","html_url":"https://github.com/caddyserver/transform-encoder","commit_stats":null,"previous_names":["caddyserver/format-encoder"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caddyserver%2Ftransform-encoder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caddyserver%2Ftransform-encoder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caddyserver%2Ftransform-encoder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caddyserver%2Ftransform-encoder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/caddyserver","download_url":"https://codeload.github.com/caddyserver/transform-encoder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647255,"owners_count":21139081,"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-13T10:41:52.400Z","updated_at":"2025-04-12T23:38:12.344Z","avatar_url":"https://github.com/caddyserver.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Transform Encoder Module for Caddy's Logger\n===============================================\n\nThis module adds logging encoder named `transform`. The module accepts a `template` with the placeholders are surrounded\nby\nbraces `{}` and filled by values extracted from the stucture of the JSON log encoder. The JSON configuration looks like\nthis:\n\n```json\n{\n  \"encoder\": \"transform\",\n  \"template\": \"{...}\"\n}\n```\n\nThe nesting is traversed using `\u003e`. For example, to print the `uri` field, the traversal is templated as `{request\u003euri}`\n.\n\n```json\n{\n  \"request\": {\n    \"method\": \"GET\",\n    \"uri\": \"/\",\n    \"proto\": \"HTTP/2.0\",\n    ...\n  }\n```\n\nThe Caddyfile configuration accepts the template immediately following the encoder name, and can be ommitted to assume\nApache Common Log Format. The body of the block accepts the custom `placeholder` property in addition to all the properties listed within the [format modules section](https://caddyserver.com/docs/caddyfile/directives/log#format-modules).\n\n```caddyfile\nlog {\n\tformat transform [\u003ctemplate\u003e] {\n\t\tplaceholder \u003cstring\u003e\n\t\tunescape_strings\n\t\t# other fields accepted by JSON encoder\n\t}\n}\n```\n\nThe syntax of `template` is defined by the package [github.com/buger/jsonparser](https://github.com/buger/jsonparser).\nObjects are traversed using the key name. Arrays can be traversed by using the format `[index]`, as in `[0]`. For\nexample, to get the first element in the `User-Agent` array, the template is `{request\u003eheaders\u003eUser-Agent\u003e[0]}`.\n\n## Examples\n\n### Apache Common Log Format Example\n\nThe module comes with one special value of `{common_log}` for the Apache Common Log format to simplify configuration\n\n```caddyfile\n format transform \"{common_log}\"\n```\n\nThe more spelled out way of doing it is:\n\n```caddyfile\nformat transform `{request\u003eremote_ip} - {user_id} [{ts}] \"{request\u003emethod} {request\u003euri} {request\u003eproto}\" {status} {size}` {\n\ttime_format \"02/Jan/2006:15:04:05 -0700\"\n}\n```\n\n### Apache Combined Log Format Example\n\nThe more spelled out way of doing it is:\n\n```caddy\nformat transform `{request\u003eremote_ip} - {user_id} [{ts}] \"{request\u003emethod} {request\u003euri} {request\u003eproto}\" {status} {size} \"{request\u003eheaders\u003eReferer\u003e[0]}\" \"{request\u003eheaders\u003eUser-Agent\u003e[0]}\"` {\n        time_format \"02/Jan/2006:15:04:05 -0700\"\n}\n```\n\n# Alternative value\n\nYou can use an alternative value by using the following syntax `{val1:val2}`. For example, to show the `X-Forwarded-For`\nheader as `remote_ip` replacement you can do the following\n\n```caddy\nformat transform `{request\u003eheaders\u003eX-Forwarded-For\u003e[0]:request\u003eremote_ip} - {user_id} [{ts}] \"{request\u003emethod} {request\u003euri} {request\u003eproto}\" {status} {size} \"{request\u003eheaders\u003eReferer\u003e[0]}\" \"{request\u003eheaders\u003eUser-Agent\u003e[0]}\"` {\n        time_format \"02/Jan/2006:15:04:05 -0700\"\n}\n```\n\nThe character `:` act as indicator for alternative value if the preceding key is not set.\n\nFor example, `{request\u003eheaders\u003eX-Forwarded-For\u003e[0]:request\u003eremote_ip}` means that if `X-Forwarded-For` first array value is not empty use that otherwise fallback on `remote_ip`.\n\n## Install\n\nFirst, the [xcaddy](https://github.com/caddyserver/xcaddy) command:\n\n```shell\n$ go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest\n```\n\nThen build Caddy with this Go module plugged in. For example:\n\n```shell\n$ xcaddy build --with github.com/caddyserver/transform-encoder\n```\n\nAlternatively, Caddy 2.4.4 and up supports adding a module directly:\n```shell\n$ caddy add-package github.com/caddyserver/transform-encoder\n```\n\nNote that in all cases Caddy should be stopped and started to get the module loaded, and future updates should be conducted through either xcaddy (if built with xcaddy) or `caddy upgrade` (if added with `add-package`).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaddyserver%2Ftransform-encoder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcaddyserver%2Ftransform-encoder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaddyserver%2Ftransform-encoder/lists"}