What is the difference between "named log" in Caddyfile global options and log in Site Block?

0. TL;DR

What is the difference between “named log” in Caddyfile Global Options and log in Site Block? How to use the “named log” defined in the global options in Site Block?

I found that the log in Global Options can use the “include” command, but the log in the Site Block can not be used; I need to print the upstream address in the reverse proxy, how do I configure the log in the Site Block?

1. Caddy version (caddy version):

v2.4.3

2. How I run Caddy:

docker compose

a. System environment:

Ubuntu 20.04

b. Command:

docke-compose up -d

c. Service/unit/compose file:

version: '3.8'
services:
  caddy:
    image: mritd/caddy
    container_name: caddy
    restart: always
    environment:
      TZ: Asia/Shanghai
    volumes:
      - /etc/timezone:/etc/timezone
      - ./conf:/etc/caddy
      - config:/config
      - data:/data
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
      - "2019:2019"
volumes:
  config:
  data:

d. My complete Caddyfile or JSON config:

{
    log test {
        format json {
            time_format "iso8601"
        }
        level DEBUG
        include "http.handlers.reverse_proxy"
        exclude "http.handlers"
    }
}

abc.com {
    log {
       output file "abc.com.log" {
            roll_size 100mb
            roll_keep 3
            roll_keep_for 7d
        }
    }
}

3. The problem I’m having:

How to use named log, such as “test” log?

4. Error messages and/or full log output:

no output

5. What I already tried:

6. Links to relevant resources:

The log directive inside of a site block only enables logging (i.e. include) for http.log.access.logN where N is some generated number to uniquely identify the logger from different site blocks. The “name” of those loggers end up being logN, similarly. You can see this by running caddy adapt --pretty to see the JSON representation of your Caddyfile config.

The global log option allows you to more generally define logging.

There’s a special name default which is always enabled by default which outputs to stderr. You can override the default logger to make Caddy stop emitting to stderr there and output elsewhere. Omitting the <name> is the same as having specified default.

Using a differently named global logger will let you leave the stderr output alone, but define separate loggers which can be more targeted.

In your case, you don’t need to configure both include and exclude, simply specifying include is enough, since it’s essentially an allow-list.

You didn’t specify an output for that log, so Caddy defaults to stderr for that logger.

1 Like

Can I understand it as the site log recorded by “Named log” in Global Options and output it to a file?

I don’t understand your question. Could you try rewording it?

Sorry, let me re-describe my question:

I expect to print the upstream address of the reverse proxy in the Site Block, but I found that “include” is not allowed in the log directive of the Site Block.

Is there a way to print the upstream address in the Site Block?

If you want different log files per site, no. Access logs have special logic to split them up by the hostname of the request.

I like to think of the log in the global block as the process log for Caddy and the log in the site block as being the access log for the site.

2 Likes

But I want to print the upstream ip of reverse_proxy in the access log. The upstream ip information of reverse_proxy is not available in the access log. Is there any way?

It’s not possible on a per site basis, but if you send all your access logs to the same file as you do your reverse_proxy logs, yes it is possible (you’d have all your access logs and proxy debug logs mixed in the same file).

1 Like

Okay, thanks for your answers, I will try this way.

This topic was automatically closed after 30 days. New replies are no longer allowed.