Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Fixed

- `tt log -f`: possible line loss/duplication on rename, hanging after
a watched log directory is removed, and lines written just as the
file was read to the end not showing up until the next write.

## [2.13.0] - 2026-05-21

This release adds cluster worker configuration management and fixes
Expand Down
53 changes: 51 additions & 2 deletions cli/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"sync"
"time"

"github.com/spf13/cobra"
"github.com/tarantool/tt/cli/cmd/internal"
Expand All @@ -20,6 +22,8 @@ var logOpts struct {
follow bool // Follow logs output.
}

const logRootCheckInterval = 100 * time.Millisecond

// NewLogCmd creates log command.
func NewLogCmd() *cobra.Command {
logCmd := &cobra.Command{
Expand Down Expand Up @@ -60,10 +64,46 @@ func printLines(ctx context.Context, in <-chan string) error {
}
}

type logRootContext struct {
ctx context.Context
cancel context.CancelFunc
}

func logRoot(inst running.InstanceCtx) string {
if inst.SingleApp {
return inst.LogDir
}
return filepath.Dir(inst.LogDir)
}

func monitorLogRoot(ctx context.Context, root string, cancel context.CancelFunc) {
ticker := time.NewTicker(logRootCheckInterval)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if _, err := os.Stat(root); errors.Is(err, os.ErrNotExist) {
cancel()
return
}
}
}
}

func follow(instances []running.InstanceCtx, n int) error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

rootContexts := make(map[string]logRootContext)
defer func() {
for _, rootCtx := range rootContexts {
rootCtx.cancel()
}
}()

nextColor := tail.DefaultColorPicker()
color := nextColor()
const logLinesChannelCapacity = 64
Expand All @@ -72,9 +112,18 @@ func follow(instances []running.InstanceCtx, n int) error {
// Wait group to wait for completion of all log reading routines to close the channel once.
var wg sync.WaitGroup
for _, inst := range instances {
if err := tail.Follow(ctx, logLines,
root := logRoot(inst)
rootCtx, ok := rootContexts[root]
if !ok {
rootCtx.ctx, rootCtx.cancel = context.WithCancel(ctx)
rootContexts[root] = rootCtx
go monitorLogRoot(rootCtx.ctx, root, rootCtx.cancel)
}

err := tail.Follow(rootCtx.ctx, logLines,
tail.NewLogFormatter(running.GetAppInstanceName(inst)+": ", color),
inst.Log, n, &wg); err != nil {
inst.Log, n, &wg)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}
Expand Down
49 changes: 49 additions & 0 deletions cli/cmd/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cmd

import (
"context"
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/tarantool/tt/cli/running"
)

func TestLogRoot(t *testing.T) {
t.Run("multi-instance application", func(t *testing.T) {
inst := running.InstanceCtx{
LogDir: filepath.Join("app", "var", "log", "instance"),
}
expected := filepath.Join("app", "var", "log")
actual := logRoot(inst)
require.Equal(t, expected, actual)
})

t.Run("single-instance application", func(t *testing.T) {
inst := running.InstanceCtx{
LogDir: filepath.Join("var", "log"),
SingleApp: true,
}
actual := logRoot(inst)
require.Equal(t, inst.LogDir, actual)
})
}

func TestMonitorLogRoot(t *testing.T) {
root := t.TempDir()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go monitorLogRoot(ctx, root, cancel)

err := os.Remove(root)
require.NoError(t, err)

select {
case <-ctx.Done():
case <-time.After(time.Second):
require.Fail(t, "log root removal did not cancel the follow context")
}
}
2 changes: 1 addition & 1 deletion cli/tail/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"

"github.com/apex/log"
"github.com/nxadm/tail"
"github.com/tarantool/go-tail"
)

const (
Expand Down
Loading
Loading