dlog

package module
v0.0.0-...-4fb5f82 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2017 License: Apache-2.0 Imports: 3 Imported by: 2

README

dlog

Go library to parse the binary Docker Logs stream into plain text.

GoDoc Build Status Coverage Status Go Report Card

dlog offers a single method: NewReader(r io.Reader) io.Reader. You are supposed to give the response body of the /containers/<id>/logs. The returned reader strips off the log headers and just gives the plain text to be used.

Here is how a log line from container looks like in the the raw docker logs stream:

01 00 00 00 00 00 00 1f 52 6f 73 65 73 20 61 72  65 ...
│  ─────┬── ─────┬─────  R  o  s  e  s     a  r   e ...
│       │        │
└stdout │        │
        │        └─ 0x0000001f = log message is 31 bytes
      unused

You can get the logs stream from go-dockerclient's Logs() method, or by calling the container logs endpoint direclty via the UNIX socket directly.

See example_test.go for an example usage.

This library is written in vanilla Go and has no external dependencies.


Licensed under Apache 2.0. Copyright 2017 Ahmet Alp Balkan.

Documentation

Overview

Package dlog provides utilities to read Docker Logs API stream format.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewReader

func NewReader(r io.Reader) io.Reader

NewReader returns a reader that strips off the message headers from the underlying raw docker logs stream and returns the messages.

Example
package main

import (
	"bufio"
	"context"
	"fmt"
	"log"
	"net"
	"net/http"

	"github.com/ahmetalpbalkan/dlog"
)

func main() {
	client := &http.Client{
		Transport: &http.Transport{
			DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
				conn, err := net.Dial("unix", "/var/run/docker.sock")
				if err != nil {
					return nil, fmt.Errorf("cannot connect docker socket: %v", err)
				}
				return conn, nil
			}}}
	url := "http://-/containers/CONTAINER_NAME/logs?stdout=1&stderr=1&follow=1"
	resp, err := client.Get(url)
	if err != nil {
		log.Fatalf("request failed: %v", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		log.Fatalf("unexpected status code: %s", resp.Status)
	}

	// At this point we have a logs stream, here is how to read each log line
	// from container:
	r := dlog.NewReader(resp.Body)
	s := bufio.NewScanner(r)
	for s.Scan() {
		log.Println(s)
	}
	if err := s.Err(); err != nil {
		log.Fatalf("read error: %v", err)
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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