oslog_collector

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2025 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

oslog-collector

Motivation

macOS OS Log (also known as Unified logs) is not saved in text format on the file system, so some log aggregator/forwarder softwares may not support it.
This software saves the OS Log obtained via the log command in a file in ndjson format so that it can be collected by other log collection tools.

Install

$ brew tap mrtc0/tap
$ brew install mrtc0/tap/oslog-collector

Configuration

When installed with Homebrew, the configuration file is placed in /opt/homebrew/etc/oslog-collector.conf.

$ cat /opt/homebrew/etc/oslog-collector.conf
pid_file: oslog-collector.pid
collectors:
  # example: collect com.apple.mdns logs
  - name: mdns
    predicate: "subsystem == 'com.apple.mdns'"
    output_file: /opt/homebrew/var/log/oslog-mdns.json
    position_file: /opt/homebrew/var/log/oslog-mds.pos
    interval: 30 # seconds

Launch and Stop

$ brew services start oslog-collector
$ brew services stop oslog-collector

oslog-collector logs are output to (brew --prefix)/var/log/oslog-collector.log.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	LogCommandTimeFormat = "2006-01-02 15:04:05"
)

Functions

func MakeReopenLogCh

func MakeReopenLogCh() chan struct{}

MakeReopenLogCh creates a channel that will receive a value when the process receives a SIGUSR1 signal. Some log rotate tools like newsyslog do not support copytruncate, so the old log file remains open. When receiving the USR1 signal (30), reopen log collector's log file to handle this.

func MakeShutdownCh

func MakeShutdownCh() chan struct{}

MakeShutdownCh creates a channel that will be closed when the process receives a SIGINT or SIGTERM signal.

func NewLogCommandBuilder

func NewLogCommandBuilder() *logCommandBuilder

func StartLogCollectors

func StartLogCollectors(ctx context.Context, collectors []*OSLogCollector)

StartLogCollectors starts the OS Log collectors in the background. It will run until the context is canceled.

Types

type Agent

type Agent struct {
	Config        *Config
	LogCollectors []*OSLogCollector

	ReopenLogCh chan struct{}
	ShutdownCh  chan struct{}
}

func NewAgentFromConfigFile

func NewAgentFromConfigFile(configFilePath string) (*Agent, error)

func (*Agent) Run

func (a *Agent) Run() error

type Config

type Config struct {
	// Collectors is a list of OS Log collectors
	Collectors []OSLogCollectorConfig `yaml:"collectors"`
	// PIDFile is the file to write the process ID to
	PIDFile string `yaml:"pid_file"`
}

func LoadConfigFromFile

func LoadConfigFromFile(filename string) (*Config, error)

func ParseConfig

func ParseConfig(rawConfig []byte) (*Config, error)

type LogCommandRunner

type LogCommandRunner interface {
	RunLogCommand() ([]byte, error)
}

func NewLogCommandRunner

func NewLogCommandRunner(args []string) LogCommandRunner

type LogCommandRunnerGenerator

type LogCommandRunnerGenerator func(args []string) LogCommandRunner

type OSLogCollector

type OSLogCollector struct {
	Name          string
	Predicate     string
	OutputFile    string
	PositionFile  string
	Interval      int
	LastTimestamp string
	WithInfoLevel bool
	// contains filtered or unexported fields
}

func NewOSLogCollector

func NewOSLogCollector(config OSLogCollectorConfig, opts ...OSLogCollectorOption) (*OSLogCollector, error)

func (*OSLogCollector) CollectLogs

func (c *OSLogCollector) CollectLogs() error

func (*OSLogCollector) OpenLogFile

func (c *OSLogCollector) OpenLogFile() error

type OSLogCollectorConfig

type OSLogCollectorConfig struct {
	// Name is the name of the collector
	Name string `yaml:"name"`
	// Predicate is the condition to get logs, which is a string passed to the --predicate option of the log command
	Predicate string `yaml:"predicate"`
	// OutputFile is the file to write the logs to
	OutputFile string `yaml:"output_file"`
	// PositionFile is the file to record the collection position of the logs
	// If this file exists, logs are collected from the position recorded in this file.
	PositionFile string `yaml:"position_file"`
	// Interval is the interval to collect logs in seconds
	Interval int `yaml:"interval"`
	// WithInfoLevel is a flag to enable the --info option of the log command
	WithInfoLevel bool `yaml:"with_info_level"`
}

type OSLogCollectorOption

type OSLogCollectorOption func(*OSLogCollector)

func WithLogCommandRunner

func WithLogCommandRunner(generator LogCommandRunnerGenerator) OSLogCollectorOption

type Position

type Position struct {
	LastTimestamp string `json:"last_timestamp"`
}

Directories

Path Synopsis
cmd
oslog-collector command

Jump to

Keyboard shortcuts

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