Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: testing

on:
push:
branches: [ master ]
pull_request:
branches: [ '**' ]
workflow_dispatch:

jobs:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a linter job too. See go-discovery/go-storage/go-tarantool etc.

test:
name: Go tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.24.x'
check-latest: true

- name: Download deps
run: go mod download

- name: Run tests
run: make test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# IDE
.idea

coverage.out
48 changes: 48 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
run:
timeout: 5m
modules-download-mode: readonly

issues:
exclude-use-default: false
max-same-issues: 0
max-issues-per-linter: 0

linters:
enable:
- govet
- staticcheck
- revive
- errcheck
- gosimple
- ineffassign
- unused
- typecheck
- gofmt
- goimports
- misspell
- gocritic

linters-settings:
revive:
severity: warning
rules:
- name: unused-parameter
- name: unreachable-code
- name: indent-error-flow
- name: exported
- name: blank-imports
- name: var-declaration
- name: if-return

misspell:
locale: US

goimports:
local-prefixes: github.com/tarantool/go-tlog

# Skip linters on example files and any generated code
exclude-rules:
- path: ^_examples/
linters: [revive, staticcheck, govet, gocritic]
- path: \.pb\.go$|_gen\.go$
linters: [all]
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added
- Core structured logging library for Go
- Support for log levels: Trace, Debug, Info, Warn, Error
- Text and JSON output formats
- Multiple output destinations: stdout, stderr, file paths, multi-target
- Automatic timestamp, source file and line number
- Stacktrace collection for error-level messages
- Test suite for core functionality
- Idiomatic Go examples (testable examples)
- Makefile, GitHub Actions CI workflow, README, LICENSE, lint configuration

### Changed

### Fixed
25 changes: 25 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
BSD 2-Clause License

Copyright (c) 2025, Tarantool AUTHORS
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 changes: 50 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Default Go toolchain
GO ?= go
GOLANGCI_LINT ?= golangci-lint
PKG := github.com/tarantool/go-tlog

.PHONY: all test test-race test-coverage lint fmt tidy examples help

all: test

## Run tests
test:
$(GO) test ./...

## Run tests with race detector
test-race:
$(GO) test -race ./...

## Run tests with coverage
test-coverage:
$(GO) test -covermode=atomic -coverprofile=coverage.out ./...

## Run golangci-lint
lint:
$(GOLANGCI_LINT) run ./...

## Format source code
fmt:
$(GO) fmt ./...

## Tidy go.mod / go.sum
tidy:
$(GO) mod tidy

## Run all _examples to ensure they compile and run without panic
examples:
$(GO) run ./_examples/stdout
$(GO) run ./_examples/stderr >/dev/null 2>&1 || true
$(GO) run ./_examples/file
$(GO) run ./_examples/multi

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Run all _examples to ensure they compile and run without panic
examples:
$(GO) run ./_examples/stdout
$(GO) run ./_examples/stderr >/dev/null 2>&1 || true
$(GO) run ./_examples/file
$(GO) run ./_examples/multi


## Show available targets
help:
@echo "Available targets:"
@echo " make test - run tests"
@echo " make test-race - run tests with -race"
@echo " make test-coverage - run tests with coverage"
@echo " make lint - run golangci-lint"
@echo " make fmt - format sources (gofmt)"
@echo " make tidy - go mod tidy"
@echo " make examples - run all examples"
178 changes: 178 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,179 @@
<a href="http://tarantool.org">
<img src="https://avatars2.githubusercontent.com/u/2344919?v=2&s=250" align="right">
</a>

[![Actions Status][actions-badge]][actions-url]
[![Telegram EN][telegram-badge]][telegram-en-url]
[![Telegram RU][telegram-badge]][telegram-ru-url]

## Table of contents

- [go-tlog](#go-tlog)
- [Features](#features)
- [Installation](#installation)
- [Quick start](#quick-start)
- [Configuration](#configuration)
- [type Opts](#type-opts)
- [Main API](#main-api)
- [Log levels](#log-levels)
- [Output formats](#output-formats)
- [Output destinations](#output-destinations)
- [Examples](#examples)
- [Testing](#testing)
- [License](#license)

# go-tlog

`go-tlog` is a lightweight and configurable logging library for Go applications.
It provides structured logging with multiple output destinations, flexible formatting,
and fine-grained log-level control.

---

## Features

- Simple setup via configuration struct
- Text or JSON output formats
- Multiple output targets: **stdout**, **stderr**, **files**
- Log levels: `Trace`, `Debug`, `Info`, `Warn`, `Error`
- Automatic timestamp, source file, and line number
- Stacktrace for errors

---

## Installation

```bash
go get github.com/tarantool/go-tlog@latest
```

Then import:

```go
import "github.com/tarantool/go-tlog"
```

---

## Quick start

```go
package main

import "github.com/tarantool/go-tlog"

func main() {
log, err := tlog.New(tlog.Opts{
Level: tlog.LevelInfo,
Format: tlog.FormatText,
Path: "stdout",
})
if err != nil {
panic(err)
}
defer log.Close()

logger := log.Logger().With(tlog.String("component", "demo"))
logger.Info("service started", "port", 8080)
logger.Error("failed to connect", "err", "timeout")
}
```

Output:

```
2025-11-10T13:30:01+05:00 INFO service started component=demo port=8080
2025-11-10T13:30:01+05:00 ERROR failed to connect err=timeout component=demo stacktrace="..."
```

---

## Configuration

### `type Opts`

```go
type Opts struct {
Level Level // minimal log level
Format Format // FormatText or FormatJSON
Path string // comma-separated outputs: "stdout,/var/log/app.log"
}
```

### Main API

| Function | Description |
|------------------|------------------------------------------|
| `tlog.New(opts)` | Create a new logger |
| `Logger()` | Return the underlying logger for use |
| `Close()` | Flush buffers and close file descriptors |

---

## Log levels

| Level | When to use |
|---------|---------------------------------------------|
| `Trace` | Low-level tracing |
| `Debug` | Debugging information |
| `Info` | Normal operational messages |
| `Warn` | Non-fatal warnings |
| `Error` | Errors and exceptions (includes stacktrace) |

---

## Output formats

| Format | Example |
|--------------|---------------------------------------------------------------|
| `FormatText` | `2025-11-10T13:31:45+05:00 INFO message key=value` |
| `FormatJSON` | `{"time":"...","level":"INFO","msg":"message","key":"value"}` |

---

## Output destinations

You can specify multiple targets separated by commas:

```go
Path: "stdout,/tmp/app.log"
```

Supported targets:

- `stdout`
- `stderr`
- File paths (created automatically if not present)

---

## Examples

Included examples:

- **ExampleNew_text** — basic text logger writing to stdout
- **ExampleNew_json** — JSON logging
- **ExampleNew_multi** — logging to multiple destinations (`stdout,/tmp/...`)

Each example demonstrates different combinations of Path, Format, and Level,
including how to log to multiple outputs at the same time.

---

## Testing

```bash
make test
```

---

## License

BSD 2-Clause License — see [LICENSE](LICENSE)

[actions-badge]: https://github.com/tarantool/go-tlog/actions/workflows/testing.yml/badge.svg
[actions-url]: https://github.com/tarantool/go-tlog/actions/workflows/testing.yml
[telegram-badge]: https://img.shields.io/badge/Telegram-join%20chat-blue.svg
[telegram-en-url]: http://telegram.me/tarantool
[telegram-ru-url]: http://telegram.me/tarantoolru
Loading