{"id":16931380,"url":"https://github.com/leodido/caddy-conditional-logging","last_synced_at":"2025-04-11T18:32:41.728Z","repository":{"id":38440017,"uuid":"385239531","full_name":"leodido/caddy-conditional-logging","owner":"leodido","description":"Caddy log encoder for conditional logging","archived":false,"fork":false,"pushed_at":"2023-03-07T01:04:50.000Z","size":398,"stargazers_count":6,"open_issues_count":7,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T14:21:53.405Z","etag":null,"topics":["caddy","caddy-plugin","caddyserver","conditional","encoder","expression-evaluator","format","if","log","logging"],"latest_commit_sha":null,"homepage":"","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/leodido.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-07-12T12:26:27.000Z","updated_at":"2023-12-06T07:14:25.000Z","dependencies_parsed_at":"2024-06-20T00:11:50.158Z","dependency_job_id":"a5f45ecf-bd53-4d44-9161-f7cecb447cc5","html_url":"https://github.com/leodido/caddy-conditional-logging","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leodido%2Fcaddy-conditional-logging","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leodido%2Fcaddy-conditional-logging/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leodido%2Fcaddy-conditional-logging/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leodido%2Fcaddy-conditional-logging/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leodido","download_url":"https://codeload.github.com/leodido/caddy-conditional-logging/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248458784,"owners_count":21107151,"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":["caddy","caddy-plugin","caddyserver","conditional","encoder","expression-evaluator","format","if","log","logging"],"created_at":"2024-10-13T20:43:51.460Z","updated_at":"2025-04-11T18:32:41.702Z","avatar_url":"https://github.com/leodido.png","language":"Go","readme":"# Caddy Conditional Logging\n\n\u003e Hey Caddy, please log only if ...\n\nThis plugin implements a logging **encoder** that let's you **log depending on conditions**.\n\nConditions can be express through a simple expression language.\n\n## Module\n\nThe **module name** is `if`.\n\nIts syntax is:\n\n```caddyfile\nif \"\u003cexpression\u003e\" [\u003cencoder\u003e]\n```\n\nThis Caddy module logs as the `\u003cencoder\u003e` demands if at least one of the expressions is met.\n\nThe `\u003cexpression\u003e` must be enclosed in double quotes.\n\nThe supported encoders are:\n\n- [`json`](https://caddyserver.com/docs/caddyfile/directives/log#json)\n- [`console`](https://caddyserver.com/docs/caddyfile/directives/log#console)\n- [`jsonselect`](https://github.com/leodido/caddy-jsonselect-encoder)\n\nWhen no `\u003cencoder\u003e` is specified, a default encoder (`console` or `json`) is automatically set up depending on the environments.\n\n### Expressions\n\nThe [language](./lang) supports simple boolean expressions.\n\nAn expression is - usually - in the form of `\u003clhs\u003e \u003coperator\u003e \u003crhs\u003e`. But you can compose and nest them!\n\nTake a look at the [language documentation](./lang/README.md) for more information.\n\n## Caddyfile\n\nLog JSON to stdout if the status starts with a 4 (eg., 404).\n\n```caddyfile\nlog {\n  output stdout\n  format if \"status ~~ `^4`\" json\n}\n```\n\nLog to stdout in console format if the request's method is \"GET\".\n\n```caddyfile\nlog {\n  output stdout\n  format if \"request\u003emethod == `GET`\" console\n}\n```\n\nLog JSON to stdout if at least one of the conditions match.\n\n```caddyfile\nlog {\n  output stdout\n  format if \"status ~~ `^4` || status ~~ `^5` || request\u003euri == `/`\" json\n}\n```\n\nLog JSON to stdout if the visit is from a Mozilla browser.\n\n```caddyfile\nlog {\n  output stdout\n  format if \"request\u003eheaders\u003eUser-Agent\u003e[0] ~~ `Gecko`\" json\n}\n```\n\nLog a JSON containing only the timestamp, the logger name, and the duration\nfor responses with HTTP status equal to 200.\n\n```caddyfile\nlog {\n  format if \"status == 200\" jsonselect \"{ts} {logger} {duration}\"\n}\n```\n\nThis outputs a nice JSON like the following one:\n\n```json\n{\"ts\":1626440165.351731,\"logger\":\"http.log.access.log0\",\"duration\":0.000198292}\n```\n\nDo you wanna log [Stackdriver](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry) entries only for 4** response status codes?\n\nLet's do it!\n\nChange the level and time format, and also change the key names for the resulting JSON.\n\n```caddyfile\nlog {\n  format if \"status ~~ `^4`\" jsonselect \"{severity:level} {timestamp:ts} {logName:logger} {httpRequest\u003erequestMethod:request\u003emethod} {httpRequest\u003eprotocol:request\u003eproto} {httpRequest\u003estatus:status} {httpRequest\u003eresponseSize:size} {httpRequest\u003euserAgent:request\u003eheaders\u003eUser-Agent\u003e[0]} {httpRequest\u003erequestUrl:request\u003euri}\" {\n    level_format \"upper\"\n    time_format \"rfc3339_nano\"\n  }\n}\n```\n\nThis outputs:\n\n```json\n{\"severity\":\"INFO\",\"timestamp\":\"2021-07-19T15:44:44.077586Z\",\"logName\":\"http.log.access.log0\",\"httpRequest\":{\"requestMethod\":\"GET\",\"protocol\":\"HTTP/2.0\",\"status\":200,\"responseSize\":11348,\"userAgent\":\"Mozilla/5.0 ...\",\"requestUrl\":\"/leo\"}}\n```\n\n## Try it out\n\nFrom the root directoy of this project, run:\n\n```console\nxcaddy run\n```\n\nThen open \u003chttps://localhost:2015\u003e, go on existing and non-existing pages, and observe the access logs.\n\nTo install xcaddy in case you need to, run:\n\n```console\ngo get -u github.com/caddyserver/xcaddy/cmd/xcaddy\n```\n\n## Build\n\nTo build [Caddy](https://github.com/caddyserver/caddy) with this module in, execute:\n\n```console\nxcaddy build --with github.com/leodido/caddy-conditional-logging\n```\n\n---\n\n[![Analytics](https://ga-beacon.appspot.com/UA-49657176-1/caddy-conditional-logging?flat)](https://github.com/igrigorik/ga-beacon)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleodido%2Fcaddy-conditional-logging","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleodido%2Fcaddy-conditional-logging","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleodido%2Fcaddy-conditional-logging/lists"}