logdna-go

module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2020 License: MIT

README

Go library for logging to LogDNA

CircleCI Coverage Status GoDoc

🚧 Work in progress 🚧


Install

go get github.com/logdna/logdna-go

Setup

import (
    "github.com/logdna/logdna-go/logger"
)

func main() {
    key := "YOUR INGESTION KEY HERE"

    // Configure your options with your desired level, hostname, app, ip address, mac address and environment. 
    // Hostname is the only required field in your options- the rest are optional.
    options := logger.Options{}
    options.Level = "fatal"
    options.Hostname = "gotest"
    options.App = "myapp"
    options.IPAddress = "10.0.1.101"
    options.MacAddress = "C0:FF:EE:C0:FF:EE"
    options.Env = "production"
    options.Tags = "logging,golang"

    myLogger, err := logger.CreateLogger(options, key)
}

Required

Usage

After initial setup, logging looks like this:

func main() {
    ...
    myLogger, err := logger.CreateLogger(options, key)
    myLogger.Log("Message 1")
    myLogger.Close()

    // Can also use Go's short-hand syntax for initializing structs to define all your options in just a single line:
    options = logger.Options{Level: "error", Hostname: "gotest", App: "myapp", IPAddress: "10.0.1.101", MacAddress: "C0:FF:EE:C0:FF:EE"}
    myLogger2, err := logger.CreateLogger(options, key)
    myLogger2.Log("Message 2")

    // Configure options with specific logs
    newOptions := logger.Options{Level: "warning", Hostname: "gotest", App: "myotherapp", IPAddress: "10.0.1.101", MacAddress:  "C0:FF:EE:C0:FF:EE"}
    errWithOpts := myLogger2.LogWithOptions("Message 3", newOptions)

    // We support the following 6 levels
    myLogger2.Info("Message 1")
    myLogger2.Warn("Message 2")
    myLogger2.Debug("Message 3")
    myLogger2.Error("Message 4")
    myLogger2.Fatal("Message 5")
    myLogger2.Critical("Message 6")

    // To add metadata to every log-line created by the logger instance:
    options.Meta = `{"key": "value", "key2": "value2"}`
    myLogger3, err := logger.CreateLogger(options, key)
    myLogger3.Log("Message 7")
    myLogger3.Close()
}

You will see these logs in your LogDNA dashboard! Make sure to run .Close() when done with using the logger.

Tests

Run all tests in the test suite:

go test ./logger

Run a specific test:

go test ./logger -run ^TestLogger_LogWithOptions$

For more information on testing see: https://golang.org/pkg/testing/

API

NewLogger(Options, Key)


Options
App
  • Optional
  • Type: string
  • Default: ''
  • Example Values: YourCustomApp
  • Max Length: 80

Arbitrary app name for labeling each message.

Env
  • Optional
  • Type: string
  • Default: ''
  • Example Values: YourCustomEnvironment
  • Max Length: 80

An environment label attached to each message.

FlushInterval
  • Optional
  • Type: time.duration
  • Default: 250 * time.Millisecond
  • Example Values: 10 * time.Second

Time to wait before sending the buffer.

Hostname
  • Optional
  • Type: string
  • Default: ''
  • Example Values: YourCustomHostname
  • Max Length: 80

Hostname for each HTTP request.

IndexMeta
  • Optional
  • Type: bool
  • Default: false
  • Example Values: true

Controls whether meta data for each message is searchable.

IngestURL
  • Optional
  • Type: string
  • Default: https://logs.logdna.com/logs/ingest

URL of the logging server.

IPAddress
  • Optional
  • Type: string
  • Default: ''
  • Example Values: 10.0.0.1

IPv4 or IPv6 address for each HTTP request.

Level
  • Optional
  • Type: string
  • Default: Info
  • Example Values: Debug, Trace, Info, Warn, Error, Fatal, YourCustomLevel
  • Max Length: 80

Level to be used if not specified elsewhere.

MacAddress
  • Optional
  • Type: string
  • Default: ''
  • Example Values: c0:ff:ee:c0:ff:ee

MAC address for each HTTP request.

MaxBufferLen
  • Optional
  • Type: int
  • Default: 50
  • Example Values: 10

Maximum total line lengths before a flush is forced.

Meta
  • Optional
  • Type: string

Global metadata. Added to each message, unless overridden.

SendTimeout
  • Optional
  • Type: time.Duration
  • Default: 30 * time.Second
  • Example Values: 10

Time limit in seconds to wait for each HTTP request before timing out.

Tags
  • Optional
  • Type: string
  • Default: 5
  • Example Values: logging,golang

Tags to be added to each message.

Timestamp
  • Optional
  • Type: time.Time
  • Default Values: time.Now()
  • Example Values: time.Now()

Epoch ms time to use if not provided elsewhere.


Log(Message)

Message
  • Required
  • Type: string
  • Default: ''

Text of the log entry.


LogWithOptions(Message, Options)

Message
  • Required
  • Type: string
  • Default: ''

Text of the log entry.

Options
App
  • Optional
  • Type: string
  • Default: ''
  • Example Values: YourCustomApp
  • Max Length: 80

App name to use for the current message.

Env
  • Optional
  • Type: string
  • Default: ''
  • Example Values: YourCustomEnvironment
  • Max Length: 80

Environment name to use for the current message.

FlushInterval
  • Optional
  • Type: time.duration
  • Default: 250 * time.Millisecond
  • Example Values: 10 * time.Second

Time to wait before sending the buffer.

Hostname
  • Optional
  • Type: string
  • Default: ''
  • Example Values: YourCustomHostname
  • Max Length: 80

Hostname to use for the current message.

IndexMeta
  • Optional
  • Type: bool
  • Default: false
  • Example Values: true

Allows for the meta to be searchable in LogDNA.

IngestURL
  • Optional
  • Type: string
  • Default: https://logs.logdna.com/logs/ingest

URL of the logging server.

IPAddress
  • Optional
  • Type: string
  • Default: ''
  • Example Values: 10.0.0.1

IPv4 or IPv6 address for the current message.

Level
  • Optional
  • Type: string
  • Default: Info
  • Example Values: Debug, Trace, Info, Warn, Error, Fatal, YourCustomLevel
  • Max Length: 80

Desired level for the current message.

MacAddress
  • Optional
  • Type: string
  • Default: ''
  • Example Values: c0:ff:ee:c0:ff:ee

MAC address for the current message.

MaxBufferLen
  • Optional
  • Type: int
  • Default: 50
  • Example Values: 10

Maximum total line lengths before a flush is forced.

Meta
  • Optional
  • Type: string

Per-message meta data.

SendTimeout
  • Optional
  • Type: time.Duration
  • Default: 30 * time.Second
  • Example Values: 10

Time limit in seconds to wait before timing out.

Tags
  • Optional
  • Type: string
  • Default: 5
  • Example Values: logging,golang

Tags to be added for the current message.

Timestamp
  • Optional
  • Type: time.Time
  • Default Values: time.Now()
  • Example Values: time.Now()

Epoch ms time to use for the current message.


LogWithLevel(Message, Level)

Message
  • Required
  • Type: string
  • Default: ''

Text of the log entry.

Level
  • Required
  • Type: string
  • Default: ``
  • Example Values: Debug, Trace, Info, Warn, Error, Fatal, YourCustomLevel
  • Max Length: 80

Desired level for the current message.


Close()

Close must be run when done with using a logger to forward any remaining buffered logs into the LogDNA product.

License

Copyright © LogDNA, released under an MIT license. See the LICENSE file and https://opensource.org/licenses/MIT

Happy Logging!

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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