logfile

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package logfile 提供节点侧的日志文件写入:按大小滑动的单文件窗口 + 有界尾部读取。

为什么不用 logrotate:节点(精简镜像)不保证装了 logrotate,而 xray.log 是 xray 输出的 唯一副本(agent 用管道接它的 stdout/stderr)。大小滑动只需要「读尾部 + 重写一次」, 且不改变运维心智——永远只有一个文件,grep 到的就是最近的日志。

滑动语义:写入使体积超过上限时,保留文件尾部一半(按行首对齐)再继续追加; 上限 0 = 不限制(回到旧行为)。日志时效性强,故不压缩、不留多份。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Size

func Size(path string) int64

Size 返回文件体积(不存在按 0)。

func SlideNow

func SlideNow(path string, maxBytes int64) (int64, error)

SlideNow 把文件裁剪到 maxBytes 的一半(按行首对齐),返回释放字节数。 供 CLI(xray-agent logs --vacuum)与 agent 内部共用;文件小于保留量时不动。

func Tail

func Tail(path string, n int) (string, error)

Tail 读取文件末尾 n 行(有界:只从尾部按 1MB 窗口回溯,不整文件进内存)。 手动模式下没有常驻 agent 时也走这里。

Types

type Sliding

type Sliding struct {
	// contains filtered or unexported fields
}

Sliding 按大小滑动的日志 writer(并发安全,可直接塞进 io.MultiWriter)。

func Open

func Open(path string, maxBytes int64) (*Sliding, error)

Open 打开(必要时创建)日志文件;存量文件超过上限时先裁剪一次。

func (*Sliding) Close

func (w *Sliding) Close() error

Close 关闭文件(子进程退出后调用)。

func (*Sliding) Size

func (w *Sliding) Size() int64

Size 当前文件体积(字节)。

func (*Sliding) Vacuum

func (w *Sliding) Vacuum() (int64, error)

Vacuum 立即裁剪到窗口内,返回释放的字节数。

func (*Sliding) Write

func (w *Sliding) Write(p []byte) (int, error)

Write 追加日志;超过上限先滑动。写失败不改变滑动状态(宁可超限也别丢日志)。

Jump to

Keyboard shortcuts

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