log

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2020 License: Apache-2.0 Imports: 8 Imported by: 1

README

Build Status GoDoc License Go version Go version Go version

log

This extremely simple log library is intended for use with micro service applications, which have only two logging targets: the console while developing the application, and a log aggregation service in production.

This library was inspired by:

Features

  • The fmt library is not used to minimize memory allocations.
  • Logs to stderr by default.
  • Can be configured to send messages to a UDP log aggregator.

Usage

By default, log will emit messages on os.Stderr which is supposed to be an unbuffered stream. The destination can be changed with:

log.SetWriter(os.Stdout)

To prevent console logging from being visible at all, use the discard writer:

log.SetWriter(ioutil.Discard)

To send output to a UDP log aggregator, just set the address and port of the service as follows:

log.SetServer("10.10.10.10:8080")

Now every log message is formatted in JSON and will be sent to the aggregator:

log.Info("The quick brown fox")
// Output: {"time":1554370662469959000,"name":"main","level":"INFO","message":"The quick brown fox"} 

Contributing

  1. Fork it
  2. Create a feature branch (git checkout -b new-feature)
  3. Commit changes (git commit -am "Added new feature xyz")
  4. Push the branch (git push origin new-feature)
  5. Create a new pull request.

Maintainers

License

Copyright 2019 MediaExchange.io

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Index

Constants

View Source
const (
	ISO8601Micro = "2006-01-02T15:04:05.000000Z0700"
)

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, fields ...Field)

Debug emits a message with the DEBUG level.

func Error

func Error(msg string, fields ...Field)

Error emits a message with the ERROR level.

func Info

func Info(msg string, fields ...Field)

Info emits a message with the INFO level.

func SetLevel

func SetLevel(l Level)

SetLevel changes the minimum level that is emitted. This is used to prevent logging from becoming too noisy. By default the minimum level is set to INFO so that DEBUG messages don't overwhelm the aggregator.

func SetServer

func SetServer(address string)

SetServer starts a UDP client that sends messages to a log aggregation server. This occurs in parallel with console logging.

func SetWriter

func SetWriter(w io.Writer)

SetWriter sets the output for all future log messages not bound for an aggregator.

func Warn

func Warn(msg string, fields ...Field)

Warn emits a message with the WARN level.

Types

type Field

type Field struct {
	Name        string
	Type        reflect.Kind
	BoolValue   bool
	IntValue    int64
	StringValue string
}

Field stores a name/value pair to be formatted by the emitter.

func Bool

func Bool(name string, value bool) Field

Bool returns a Field that contains a boolean value.

func Err

func Err(value error) Field

Err returns a Field that contains the message from an error.

func Int

func Int(name string, value int) Field

Int returns a Field that contains a default integer.

func Int16

func Int16(name string, value int16) Field

Int16 returns a Field that contains an 16-bit integer

func Int32

func Int32(name string, value int32) Field

Int32 returns a Field that contains an 32-bit integer

func Int64

func Int64(name string, value int64) Field

Int64 returns a Field that contains an 64-bit integer

func Int8

func Int8(name string, value int8) Field

Int8 returns a Field that contains an 8-bit integer

func String

func String(name string, value string) Field

String returns a Field that contains a string.

func (Field) Json

func (field Field) Json() string

Json returns the contents of the Field as `"key":value`.

func (Field) String

func (field Field) String() string

String returns the contents of the Field as `key=value`.

type Level

type Level int

Type that defines the logging level

const (
	DEBUG Level = iota
	INFO  Level = iota
	WARN  Level = iota
	ERROR Level = iota
)

These are the supported log levels.

func Parse

func Parse(s string) Level

func (Level) String

func (l Level) String() string

String converts a log level to its string representation

Jump to

Keyboard shortcuts

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