Custom log file not created / no logs for served site

1. Caddy version (caddy version):

v2.2.0

2. How I run Caddy:

caddy-api on ubuntu vm

a. System environment:

ubuntu 20.04

b. Command:

systemctl start caddy-api

c. Service/unit/compose file:

# caddy-api.service
#
# For using Caddy with its API.
#
# This unit is "durable" in that it will automatically resume
# the last active configuration if the service is restarted.
#
# See https://caddyserver.com/docs/install for instructions.

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target

[Service]
User=caddy
Group=caddy
Environment="CONSUL_HTTP_ADDR=https://consul01p.net.work:8500"
ExecStart=/usr/bin/caddy run --environ --resume
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddyfile or JSON config:

This is the part I think I have trouble since everything else is working fine, proxy, tls, etc…

"logging":{
      "logs":{
         "account":{
            "encoder":{
               "format":"json"
            },
            "include":[
               "http.log.access.account"
            ],
            "level":"INFO",
            "writer":{
               "filename":"/tmp/account.log",
               "output":"file"
            }
         },
         "arkliv":{
            "encoder":{
               "format":"json"
            },
            "include":[
               "http.log.access.arkliv"
            ],
            "level":"INFO",
            "writer":{
               "filename":"/tmp/arkliv.log",
               "output":"file"
            }
         },
         "default":{
            "encoder":{
               "format":"json"
            },
            "exclude":[
               "http.log.access.account",
               "http.log.access.arkliv"
            ],
            "level":"INFO",
            "writer":{
               "filename":"/tmp/caddy.log",
               "output":"file"
            }
         }
      },
      "sink":{
         "writer":{
            "filename":"/tmp/caddy-temp.log",
            "output":"file"
         }
      }
   }

3. The problem I’m having:

As you can see I want long of a specific site going to it’s own log file.
Unfortunately nothing is created and in caddy.log or caddy-temp.log I don’t have any log showing access to this 2 sites ( as expected with exclude).

4. Error messages and/or full log output:

No error message.

5. What I already tried:

I tried to adapt a Caddyfile

exa.com
{
root * /www/exa.com
reverse_proxy /gg localhost:10000
log {
        output file /tmp/exa_access.log
}

and it did not gave me relevant informations at all except this part which turn out not working when I put it in my config.

"logs":{
                  "logger_names":{
                     "exa.com":"log0"
                  }
}

Thanks in advance,

Regards.

You only posted partial configs, please post your entire config. It’s not possible to know the problem with only fragments of information.

Did you verify that the caddy user has permissions to write to /tmp?

Hello Francis,
Like I said caddy is doing great with proxying request to those two backends but I can give you the config no problem about that.
But definition /tmp is accessible to all user of my system and yes caddy user can write thought it.
First I made it right to /var/log/caddy , and it was writing caddy-tmp.log & caddy-Go.log but no file were created for my sites ie : account/log / akrliv.log

The configuration :

{
   "admin":{
      "listen":"0.0.0.0:2019"
   },
   "apps":{
      "http":{
         "servers":{
            "prodsrv":{
               "listen":[
                  ":443"
               ],
               "routes":[
                  {
                     "handle":[
                        {
                           "handler":"reverse_proxy",
                           "transport":{
                              "protocol":"http",
                              "tls":{
                                 
                              }
                           },
                           "upstreams":[
                              {
                                 "dial":"account.net.work:443"
                              }
                           ]
                        }
                     ],
                     "match":[
                        {
                           "host":[
                              "account.net.work"
                           ]
                        }
                     ]
                  },
                  {
                     "handle":[
                        {
                           "handler":"subroute",
                           "routes":[
                              {
                                 "handle":[
                                    {
                                       "handler":"reverse_proxy",
                                       "transport":{
                                          "protocol":"http"
                                       },
                                       "upstreams":[
                                          {
                                             "dial":"serverweb01p.net.work/api/reportsext:8080"
                                          }
                                       ]
                                    }
                                 ],
                                 "match":[
                                    {
                                       "path":[
                                          "/reports"
                                       ]
                                    }
                                 ]
                              },
                              {
                                 "handle":[
                                    {
                                       "handler":"static_response",
                                       "status_code":503
                                    }
                                 ],
                                 "match":[
                                    {
                                       "path":[
                                          "/"
                                       ]
                                    }
                                 ]
                              }
                           ]
                        }
                     ],
                     "match":[
                        {
                           "host":[
                              "arkliv.net.work"
                           ]
                        }
                     ]
                  }
               ],
               "tls_connection_policies":[
                  {
                     "certificate_selection":{
                        "any_tag":[
                           "prodcert"
                        ]
                     }
                  }
               ]
            }
         }
      },
      "tls":{
         "certificates":{
            "load_files":[
               {
                  "certificate":"/etc/ssl/certs/prod/pem-prod.pem",
                  "key":"/etc/ssl/certs/prod/key-prod.key",
                  "tags":[
                     "prodcert"
                  ]
               }
            ]
         }
      }
   },
   "logging":{
      "logs":{
         "account":{
            "encoder":{
               "format":"json"
            },
            "include":[
               "http.log.access.account"
            ],
            "level":"INFO",
            "writer":{
               "filename":"/var/log/caddy/account.log",
               "output":"file"
            }
         },
         "arkliv":{
            "encoder":{
               "format":"json"
            },
            "include":[
               "http.log.access.arkliv"
            ],
            "level":"INFO",
            "writer":{
               "filename":"/var/log/caddy/arkliv.log",
               "output":"file"
            }
         },
         "default":{
            "encoder":{
               "format":"json"
            },
            "exclude":[
               "http.log.access.account",
               "http.log.access.arkliv"
            ],
            "level":"INFO",
            "writer":{
               "filename":"/var/log/caddy-Go.log",
               "output":"file"
            }
         }
      },
      "sink":{
         "writer":{
            "filename":"/var/log/caddy-tmp.log",
            "output":"file"
         }
      }
   }
}

PS : This config file is generated thought a gitlab CI/CD, adding a new sites when we push it to the git repository conf.d.
The json is then generated by a python script then push on our front servers with caddy by this cmd :

curl -v -X POST "http://frproxycaddy01p.net.work:2019/load" -H "Content-Type:application/json" -d @config.json
curl -v -X POST "http://frproxycaddy02p.net.work:2019/load" -H "Content-Type:application/json" -d @config.json

You need the app.http.servers.log fields to be filled in for each domain you want logs for.

logger_names is what makes the link between each hostname and which log configuration to use. Keys are hostnames, values are logger names.

So you’ll need for example "logs": {"logger_names": {"account.net.work": "account"}}. Add a logger_names entry for each domain).

It was indeed the problem I just did what you told me to do and it worked now, thanks for your help.

Regards,

1 Like