file.go 611 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package logger
  5. import (
  6. "os"
  7. )
  8. type File struct {
  9. writer *os.File
  10. }
  11. func NewFile(filename string) (*File, error) {
  12. file, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
  13. if err != nil {
  14. return nil, err
  15. }
  16. return &File{file}, nil
  17. }
  18. func (f *File) Write(event *Event) {
  19. f.writer.Write([]byte(event.fmsg))
  20. }
  21. func (f *File) Close() {
  22. f.writer.Close()
  23. f.writer = nil
  24. }
  25. func (f *File) Sync() {
  26. f.writer.Sync()
  27. }