github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/audit/file.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package audit
     5  
     6  import (
     7  	"os"
     8  
     9  	"github.com/wiggin77/logr"
    10  	"github.com/wiggin77/logr/target"
    11  )
    12  
    13  type FileOptions target.FileOptions
    14  
    15  // NewFileTarget creates a target capable of outputting log records to a rotated file.
    16  func NewFileTarget(filter logr.Filter, formatter logr.Formatter, opts FileOptions, maxQSize int) (*target.File, error) {
    17  	fopts := target.FileOptions(opts)
    18  	err := checkFileWritable(fopts.Filename)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	target := target.NewFileTarget(filter, formatter, fopts, maxQSize)
    23  	return target, nil
    24  }
    25  
    26  func checkFileWritable(filename string) error {
    27  	// try opening/creating the file for writing
    28  	file, err := os.OpenFile(filename, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600)
    29  	if err != nil {
    30  		return err
    31  	}
    32  	file.Close()
    33  	return nil
    34  }