File: fmt.go

package info (click to toggle)
golang-github-go-log-log 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, trixie
  • size: 120 kB
  • sloc: makefile: 2
file content (38 lines) | stat: -rw-r--r-- 728 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package fmt

import (
	"fmt"
	"io"
	"os"
	"strings"
)

type fmtLogger struct {
	writer io.Writer
}

func (t *fmtLogger) Log(v ...interface{}) {
	t.output(fmt.Sprint(v...))
}

func (t *fmtLogger) Logf(format string, v ...interface{}) {
	t.output(fmt.Sprintf(format, v...))
}

func (logger *fmtLogger) output(line string) (n int, err error) {
	if !strings.HasSuffix(line, "\n") {
		line += "\n"
	}
	return logger.writer.Write([]byte(line))
}

// New creates a new fmt logger which writes to stdout.
func New() *fmtLogger {
	return &fmtLogger{writer: os.Stdout}
}

// NewFromWriter creates a new fmt logger which writes to the given
// writer.
func NewFromWriter(writer io.Writer) *fmtLogger {
	return &fmtLogger{writer: writer}
}