Documentation ¶
Index ¶
- func Close(closers ...io.Closer) error
- func CloseSingle(closer io.Closer) error
- func CountLines(r io.Reader) (int, error)
- func CountLines1(r io.Reader) (int, error)
- func MultiReader(readers ...io.Reader) io.Reader
- func MultiWriteCloser(writeClosers ...io.WriteCloser) io.WriteCloser
- func MultiWriter(writers ...io.Writer) io.Writer
- func NewDailyRotateRuleWriteCloser(filePath, delimiter string, days int, compress bool) (io.WriteCloser, error)deprecated
- func NewFileReader(filePath string) (*os.File, error)
- func NewLumberjackWriteCloser(filePath string, options ...LumberjackOption) (io.WriteCloser, error)
- func NewLumberjackWriteCloser1(filePath string, maxSize, maxBackups, maxAge int, localTime, compress bool) (io.WriteCloser, error)deprecated
- func NewReadWriter(s []byte) *bytes.Buffer
- func NewReadWriterFromString(str string) *bytes.Buffer
- func NewReader(s []byte) io.Reader
- func NewReaderFromString(str string) io.Reader
- func NewSizeLimitRotateRuleWriteCloser(filePath, delimiter string, days, maxSize, maxBackups int, compress bool) (io.WriteCloser, error)deprecated
- func NopCloserToReadSeeker(readSeeker io.ReadSeeker) io.ReadSeekCloser
- func NopCloserToReader(reader io.Reader) io.ReadCloser
- func NopCloserToWriter(writer io.Writer) io.WriteCloser
- func ReadFromReader(reader io.Reader) ([]byte, error)
- func ToBufioReader(reader io.Reader) *bufio.Reader
- func TryToClose(objs ...any) error
- type LumberjackOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Close ¶ added in v2.1.7
Close
PS: 就算循环过程中返回了非nil的error,也要继续向下循环(尽可能多地关闭).
@param closers (1) 可以为nil(即不传参);(2) 其中可以有nil @return 发生error的话,返回第一个
func CloseSingle ¶ added in v2.1.7
func MultiWriteCloser ¶
func MultiWriteCloser(writeClosers ...io.WriteCloser) io.WriteCloser
MultiWriteCloser
PS: 参考了 io.MultiWriter().
func NewDailyRotateRuleWriteCloser
deprecated
func NewDailyRotateRuleWriteCloser(filePath, delimiter string, days int, compress bool) (io.WriteCloser, error)
NewDailyRotateRuleWriteCloser 调用了go-zero中的方法.
Deprecated: 调用完 Write() 后立即调用 Close() || 进程退出,Write的内容可能不会立即保存到文件中,导致丢失.
@param filePath 会自动创建父级目录;
文件不存在: 会创建; 文件已存在: (1)是个文件,会append;(2)是个目录,会返回error(e.g. open /Users/richelieu/Downloads: is a directory)
func NewFileReader ¶
NewFileReader
!!!:要在外部手动调用 *os.File 的Close方法.
@return os.File 结构体 实现了 io.Reader 接口
func NewLumberjackWriteCloser ¶
func NewLumberjackWriteCloser(filePath string, options ...LumberjackOption) (io.WriteCloser, error)
NewLumberjackWriteCloser
参考: https://github.com/natefinch/lumberjack
PS: (1) 传参可以参考下 NewLumberjackWriteCloser1 的注释; (2) 仅配置 filePath 的情况: (a) 超过100MB就rotate一下;
(b) 保留所有文件; (c) 不压缩; (d) 默认使用UTC时间;
(3) 文件不存在,会自动创建;文件存在,内容会追加在最后.
@param filePath 文件路径 @param options 可选配置
func NewLumberjackWriteCloser1
deprecated
func NewLumberjackWriteCloser1(filePath string, maxSize, maxBackups, maxAge int, localTime, compress bool) (io.WriteCloser, error)
NewLumberjackWriteCloser1
Deprecated: Use NewLumberjackWriteCloser instead. 唯一作用: 看如何传参.
PS: (1) lumberjack: 日志切割组件.一般情况下,lumberjack配合其他日志库,实现日志的滚动(rolling)记录. (2) 根据 maxBackups、maxAge 删除过期文件,根据 compress 决定是否压缩哪些未压缩的旧日志文件。
Golang 语言三方库 lumberjack 日志切割组件怎么使用?
https://mp.weixin.qq.com/s/gGnovwzS1ucW3Afxcytp_Q
go语言的日志滚动(rolling)记录器——lumberjack
https://zhuanlan.zhihu.com/p/430224518
@param filePath 日志路径,归档日志也会保存在对应目录下(若该值为空,则日志会保存到os.TempDir()目录下,日志文件名为<processname>-lumberjack.log)
(1) 会尝试创建父级目录; (2) 文件不存在,会自动创建; (3) 文件存在, (3.1) 是文件,内容追加在最后; (3.2) 是目录,返回error.
@param maxSize [单位: MB,默认: 100MB] 日志大小到达此值,就开始backup @param maxBackups 旧日志保存的最大数量,默认保存所有旧日志文件 @param maxAge [单位: days] 旧日志保存的最大天数,默认保存所有旧日志文件 @param localTime [默认使用UTC时间] 是否使用本地时间戳? @param compress [默认: false] 对backup的日志是否进行压缩?(压缩实际上是打成压缩包,文件名最后加上".gz")
e.g. rotate && compress == false
"aaa.log"(最新的日志文件;当前的指向) "aaa-2023-05-10T08-51-01.320.log" "aaa-2023-05-10T08-50-51.010.log"
e.g.1 rotate && compress == true
"aaa.log"(最新的日志文件;当前的指向) "aaa-2023-05-10T08-55-08.504.log.gz" "aaa-2023-05-10T08-54-57.786.log.gz"
func NewReaderFromString ¶
func NewSizeLimitRotateRuleWriteCloser
deprecated
func NewSizeLimitRotateRuleWriteCloser(filePath, delimiter string, days, maxSize, maxBackups int, compress bool) (io.WriteCloser, error)
NewSizeLimitRotateRuleWriteCloser 调用了go-zero中的方法.
Deprecated: 调用完 Write() 后立即调用 Close() || 进程退出,Write的内容可能不会立即保存到文件中,导致丢失.
PS: 最多生成文件的数量: maxBackups + 1(filePath)
@param filePath 会自动创建父级目录;
文件不存在: 会创建; 文件已存在: (1)是个文件,会append;(2)是个目录,会返回error(e.g. open /Users/richelieu/Downloads: is a directory)
@param delimiter 分隔符 @param maxSize 单位: MB @param maxBackups 备份数量的上限 @param compress 如果设置为true,compress前后各会输出一条信息(logx.Infof、logx.Errorf)
e.g. Mac环境
("test.log", "-", 1, 10, 3, true) 4个文件: test.log test-2023-03-10T16/50/11+08/00.log.gz test-2023-03-10T16/50/10+08/00.log.gz test-2023-03-10T16/50/09+08/00.log.gz
func NopCloserToReadSeeker ¶
func NopCloserToReadSeeker(readSeeker io.ReadSeeker) io.ReadSeekCloser
func NopCloserToReader ¶
func NopCloserToReader(reader io.Reader) io.ReadCloser
NopCloserToReader
PS: 返回值调用Close()将什么都不会做,直接返回nil.
func NopCloserToWriter ¶
func NopCloserToWriter(writer io.Writer) io.WriteCloser
NopCloserToWriter
PS: 返回值调用Close()将什么都不会做,直接返回nil.
func ReadFromReader ¶
ReadFromReader 读取io.Reader的内容(io.Reader => []byte)
func ToBufioReader ¶
ToBufioReader io.Reader 接口 => *bufio.Reader
作用: 可以调用 bufio.Reader 结构体的方法(因为 io.Reader 接口就一个Read方法).
func TryToClose ¶
TryToClose 尝试关闭(如果实现了io.Closer接口的话).
@param writers (1) 可以为nil(即不传参)
(2) 其中可以有nil
@return 发生error的话,返回第一个
Types ¶
type LumberjackOption ¶
type LumberjackOption func(opts *lumberjackOptions)
func WithCompress ¶
func WithCompress(compress bool) LumberjackOption
func WithConsole ¶
func WithConsole(console bool) LumberjackOption
func WithLocalTime ¶
func WithLocalTime(localTime bool) LumberjackOption
func WithMaxAge ¶
func WithMaxAge(maxAge int) LumberjackOption
func WithMaxBackups ¶
func WithMaxBackups(maxBackups int) LumberjackOption
func WithMaxSize ¶
func WithMaxSize(maxSize int) LumberjackOption