mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* MM-23222 add file target (with rotation) to audit
* MM-23222 mirror syslog audits to local filesystem
* provides config options for file name, max size, max age
* rotates files based on max size and max age; delete as needed based on max backups
* include cluster id in log records
* sort meta data fields
35 lines
893 B
Go
35 lines
893 B
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package audit
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/wiggin77/logr"
|
|
"github.com/wiggin77/logr/target"
|
|
)
|
|
|
|
type FileOptions target.FileOptions
|
|
|
|
// NewFileTarget creates a target capable of outputting log records to a rotated file.
|
|
func NewFileTarget(filter logr.Filter, formatter logr.Formatter, opts FileOptions, maxQSize int) (*target.File, error) {
|
|
fopts := target.FileOptions(opts)
|
|
err := checkFileWritable(fopts.Filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
target := target.NewFileTarget(filter, formatter, fopts, maxQSize)
|
|
return target, nil
|
|
}
|
|
|
|
func checkFileWritable(filename string) error {
|
|
// try opening/creating the file for writing
|
|
file, err := os.OpenFile(filename, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
file.Close()
|
|
return nil
|
|
}
|