logging

package module
v0.0.0-...-187974e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

Redis logger module for Caddy

This plugin sends Caddy logs to Redis using the LPUSH command, allowing other consumers to process the logs.

  • It helps address issues related to centralized log collection and the I/O and storage challenges of keeping logs locally.
  • It's particularly suited for small-scale operations deployed on VPS.
  • In the future, if scaling is needed, you can consider using Redis Cluster or DragonflyDB.

通过本插件,可把caddy日志通过LPUSH发送给redis, 以供其他消费端处理。

  • 它可以解决日志收集中心化、本地日志存储带来的io和容量问题。
  • 比较适合部署在VPS上的小规模业务
  • 未来如果要扩容,可以考虑Redis Cluster或Dragonflydb

Configuration

Simple mode

Enable Redis logger for Caddy by specifying the module configuration in the Caddyfile:

{
    order redis_logger after log
}

:80 {
    route {
        redis_logger my_redis_key {
            redis_address localhost:6379
            redis_password mypassword
            with_request_body
        }
        respond "Hello, World!"
    }
}

Note that address and db values can be configured (or accept the defaults) .

The module supports environment variable substitution within Caddyfile parameters:

{
    redis_logger my_redis_key {
        redis_address        "{$REDIS_ADDRESS}"
        redis_password       "{$REDIS_PASSWORD}"
    }
}

NOTE however the following configuration options also support runtime substition:

  • redis_db // default 0
  • dial_timeout // 连接超时时间 default 5s
  • write_timeout // 写入超时时间 default 3s
  • max_retries // 最大重试次数 default 3
Not support
  • Redis Cluster
  • Failover mode
  • TLS

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisConn

type RedisConn struct {
	net.Conn
	// contains filtered or unexported fields
}

redialerConn wraps an underlying Conn so that if any writes fail, the connection is redialed and the write is retried.

func (*RedisConn) Write

func (reconn *RedisConn) Write(b []byte) (n int, err error)

Write wraps the underlying Conn.Write method, but if that fails, it will re-dial the connection anew and try writing again.

type RedisWriter

type RedisWriter struct {
	// The address of the network socket to which to connect.
	Address string `json:"address,omitempty"`

	// The timeout to wait while connecting to the socket.
	DialTimeout caddy.Duration `json:"dial_timeout,omitempty"`

	// If enabled, allow connections errors when first opening the
	// writer. The error and subsequent log entries will be reported
	// to stderr instead until a connection can be re-established.
	SoftStart bool `json:"soft_start,omitempty"`
	// contains filtered or unexported fields
}

NetWriter implements a log writer that outputs to a network socket. If the socket goes down, it will dump logs to stderr while it attempts to reconnect.

func (RedisWriter) CaddyModule

func (RedisWriter) CaddyModule() caddy.ModuleInfo

CaddyModule returns the Caddy module information.

func (RedisWriter) OpenWriter

func (nw RedisWriter) OpenWriter() (io.WriteCloser, error)

OpenWriter opens a new network connection.

func (*RedisWriter) Provision

func (nw *RedisWriter) Provision(ctx caddy.Context) error

Provision sets up the module.

func (RedisWriter) String

func (nw RedisWriter) String() string

func (*RedisWriter) UnmarshalCaddyfile

func (nw *RedisWriter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax:

net <address> {
    dial_timeout <duration>
    soft_start
}

func (RedisWriter) WriterKey

func (nw RedisWriter) WriterKey() string

WriterKey returns a unique key representing this nw.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL