logwriter

package module
v0.0.0-...-b1385d4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2022 License: MIT Imports: 5 Imported by: 1

README

logwriter

Simple Go library for SystemD's journalctl like log formatting.

Provides time prefix generation for each line of provided messages.

Usage example

import "github.com/nxshock/logwriter"

// Create new writer that writes result to stdout
writer := logwriter.New(os.Stdout)

// Set custom time format and timezone if needed
writer.TimeFormat = "02.01.06 15:04:05"
writer.TimeZone = time.UTC

writer.Print("hello world")
// result:
// 02.01.06 15:04:05 hello world

writer.Write([]byte("line 1\nline 2\nline 3"))
// result:
// 02.01.06 15:04:05 line 1
// 02.01.06 15:04:05 line 2
// 02.01.06 15:04:05 line 3

writer.Print("hello ")
writer.Print("world")
// result:
// 02.01.06 15:04:05 hello world

writer.Close()
// writes final \n if not written before

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LogWriter

type LogWriter struct {
	TimeFormat string
	TimeZone   *time.Location
	// contains filtered or unexported fields
}

func New

func New(w io.Writer) *LogWriter

New created new LogWriter.

func (*LogWriter) Close

func (lw *LogWriter) Close() error

func (*LogWriter) Print

func (lw *LogWriter) Print(a ...any) (n int, err error)

Print formats using the default formats for its operands and writes to writer. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.

func (*LogWriter) Printf

func (lw *LogWriter) Printf(format string, a ...any) (n int, err error)

Printf formats according to a format specifier and writes to writer. It returns the number of bytes written and any write error encountered.

func (*LogWriter) Println

func (lw *LogWriter) Println(a ...any) (n int, err error)

Println formats using the default formats for its operands and writes to writer. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

func (*LogWriter) Write

func (lw *LogWriter) Write(p []byte) (n int, err error)

Write writes len(p) bytes from p to the writer. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL