Using a custom zap core

I am seeing that there is a way to specify your own log writers that implement an io.Writer to write already formatted JSON lines to log. If I would like to use my own custom zap core as a log destination, is that possible at all with the current plugin modules defined?

I don’t think so, but it’s been a couple years since I’ve looked at that core logging code. What’s your use case for a custom core?

I am currently logging all my docker and container’s logs to google cloud logging, they have a pretty nice GUI for inspecting logs and also nice alerting. Their JSON format is different from what zap normally logs, There is for example this package that works with google cloud logging together with zap: https://github.com/asahasrabuddhe/zapdriver.

Interesting; shouldn’t it be a format/encoding instead of a core just to change the output format?

This issue describes the problems in the differences: https://github.com/uber-go/zap/issues/452, it refers to an older version of zapdriver, the link I send is a different fork with a few more fixes.

I wanted to start to work on a fork for my use and I got a stumbling block:

xcaddy build --output caddy-log \
	--with github.com/caddyserver/caddy/v2=caddy-logfork \
	--with github.com/aksdb/caddy-cgi/v2

in caddy-logfork I have checked out my (still unmodified) for of caddy from GitHub - jum/caddy: Fast and extensible multi-platform HTTP/3 web server with automatic HTTPS and I get the following error:

2022/09/27 22:45:46 [INFO] Temporary folder: /tmp/buildenv_2022-09-27-2245.3471905446
2022/09/27 22:45:46 [INFO] Writing main module: /tmp/buildenv_2022-09-27-2245.3471905446/main.go
package main

import (
	caddycmd "github.com/caddyserver/caddy/v2/cmd"

	// plug in Caddy modules here
	_ "github.com/caddyserver/caddy/v2/modules/standard"
	_ "github.com/caddyserver/caddy/v2"
	_ "github.com/aksdb/caddy-cgi/v2"
)

func main() {
	caddycmd.Main()
}
2022/09/27 22:45:46 [INFO] Initializing Go module
2022/09/27 22:45:46 [INFO] exec (timeout=10s): /home/jum/go/bin/go mod init caddy
go: creating new go.mod: module caddy
go: to add module requirements and sums:
	go mod tidy
2022/09/27 22:45:46 [INFO] Replace github.com/caddyserver/caddy/v2 => caddy-logfork
2022/09/27 22:45:46 [INFO] exec (timeout=10s): /home/jum/go/bin/go mod edit -replace github.com/caddyserver/caddy/v2=caddy-logfork
go: -replace=github.com/caddyserver/caddy/v2=caddy-logfork: unversioned new path must be local directory
2022/09/27 22:45:46 [FATAL] exit status 1

Any idea what I am doing wrong? I am following the README from xcaddy.

Try replacing caddy-logfork with the absolute path to your fork.

I think you just need to do --with github.com/caddyserver/caddy/v2=./caddy-logfork i.e. add ./ to make it a relative path to a directory. Check out the examples: GitHub - caddyserver/xcaddy: Build Caddy with plugins

1 Like

That did it! Thank you.

2 Likes