Skip to content

Commit 366b1c5

Browse files
committed
tcm: show TCM logs
Added support for showing TCM logs @TarantoolBot Title: Added support for showing TCM logs This commit introduces the ability to view TCM logs using the `tt tcm log` command. This command works in couple with the `tt tcm start` command, and show the logs of the TCM process that was running by it. Command expect exists `tcm.log` file in the current directory. **Usage example:** ```bash tt tcm log --lines 100 --follow ``` This command will show the last 100 lines of the TCM log file and continue to follow the log output. This feature is useful for monitoring the TCM process. Without the `--follow` flag, it will show the last 100 lines of the log file and exit. The command also supports following flags: - `--no-color` to disable colored output - `--no-format` to disable structured output **Possible output:** ```text { time: **2025-05-29T17:19:47.905335965+03:00** level: INFO msg: _tcm stopped_ source: { file: "tcm/internal/app/tcm/server/server.go", function: "tcm/internal/server.(*TcmAPI).Close", line: 175 } } { time: **2025-05-29T17:19:47.90451709+03:00** level: WARN msg: _retrying of unary invoker failed_ attempt: 0, error: "rpc error: code = Canceled desc = latest balancer error: last connection error: connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\"", source: { file: "go.etcd.io/etcd/client/[email protected]/retry_interceptor.go", function: "v3.(*Client).unaryClientInterceptor.func1", line: 63 }, target: "etcd-endpoints://0xc0003d2000/127.0.0.1:2379" } { time: **2025-05-29T17:19:47.90548212+03:00** level: ERROR msg: _fail to get entity from etcd_ err: "context canceled", path: "/cluster/", source: { file: "tcm/internal/app/tcm/server.go", function: "tcm/internal/app/tcm/server.(*TcmAPI).validationKV", line: 17 } } ``` Closes #TNTP-1103
1 parent d7472a3 commit 366b1c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+4137
-38
lines changed

.cspell_project-words.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Svacer
3636
tarantool
3737
tarantoolctl
3838
tarantools
39+
tcm
3940
testdata
4041
testfull
4142
TNTP

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2525
- Added support for completion with shell `fish` see
2626
the command `tt completion fish`.
2727
- Repository use `pre-commit` hooks to check code style.
28+
- Added support for showing TCM logs with `tt tcm log` command.
2829

2930
### Changed
3031

cli/cmd/tcm.go

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ package cmd
33
import (
44
"errors"
55
"fmt"
6-
"log"
76
"os"
87
"os/exec"
98
"path/filepath"
109
"time"
1110

11+
"github.com/apex/log"
12+
"github.com/fatih/color"
1213
"github.com/jedib0t/go-pretty/v6/table"
1314
"github.com/jedib0t/go-pretty/v6/text"
1415
"github.com/spf13/cobra"
1516
"github.com/tarantool/tt/cli/cmdcontext"
1617
"github.com/tarantool/tt/cli/process_utils"
18+
"github.com/tarantool/tt/cli/tail"
1719
tcmCmd "github.com/tarantool/tt/cli/tcm"
1820
libwatchdog "github.com/tarantool/tt/lib/watchdog"
1921
)
@@ -23,11 +25,12 @@ var tcmCtx = tcmCmd.TcmCtx{}
2325
const (
2426
tcmPidFile = "tcm.pid"
2527
watchdogPidFile = "watchdog.pid"
28+
logFileName = "tcm.log"
2629
)
2730

2831
func newTcmStartCmd() *cobra.Command {
2932
tcmCmd := &cobra.Command{
30-
Use: "start",
33+
Use: "start [flags]",
3134
Short: "Start tcm application",
3235
Long: `Start to the tcm.
3336
tt tcm start --watchdog
@@ -36,6 +39,8 @@ func newTcmStartCmd() *cobra.Command {
3639
}
3740
tcmCmd.Flags().StringVar(&tcmCtx.Executable, "path", "", "the path to the tcm binary file")
3841
tcmCmd.Flags().BoolVar(&tcmCtx.Watchdog, "watchdog", false, "enables the watchdog")
42+
tcmCmd.Flags().StringVar(&tcmCtx.Log.Level, "log-level", "INFO",
43+
"log level for the tcm application")
3944

4045
return tcmCmd
4146
}
@@ -61,6 +66,30 @@ func newTcmStopCmd() *cobra.Command {
6166
return tcmCmd
6267
}
6368

69+
func newTcmLogCmd() *cobra.Command {
70+
cmd := &cobra.Command{
71+
Use: "log [flags]",
72+
Short: "Show tcm application logs",
73+
Long: `Show logs for the tcm. tt tcm log`,
74+
Run: RunModuleFunc(internalTcmLog),
75+
}
76+
77+
cmd.Flags().IntVarP(&tcmCtx.Log.Lines, "lines", "n", 10,
78+
"Count of last lines to output")
79+
cmd.Flags().BoolVarP(&tcmCtx.Log.IsFollow, "follow", "f", false,
80+
"Output appended data as the log file grows")
81+
cmd.Flags().BoolVar(&tcmCtx.Log.ForceColor, "color", false,
82+
"Force colored output in logs")
83+
cmd.Flags().BoolVar(&tcmCtx.Log.NoColor, "no-color", false,
84+
"Disable colored output in logs")
85+
cmd.Flags().BoolVar(&tcmCtx.Log.NoFormat, "no-format", false,
86+
"Disable log formatting")
87+
88+
cmd.MarkFlagsMutuallyExclusive("color", "no-color")
89+
90+
return cmd
91+
}
92+
6493
func NewTcmCmd() *cobra.Command {
6594
tcmCmd := &cobra.Command{
6695
Use: "tcm",
@@ -70,12 +99,19 @@ func NewTcmCmd() *cobra.Command {
7099
newTcmStartCmd(),
71100
newTcmStatusCmd(),
72101
newTcmStopCmd(),
102+
newTcmLogCmd(),
73103
)
74104
return tcmCmd
75105
}
76106

77-
func startTcmInteractive() error {
78-
tcmApp := exec.Command(tcmCtx.Executable)
107+
func startTcmInteractive(logLevel string) error {
108+
tcmApp := exec.Command(tcmCtx.Executable,
109+
"--log.default.add-source",
110+
"--log.default.output=file",
111+
"--log.default.format=json",
112+
"--log.default.level="+logLevel,
113+
"--log.default.file.name="+logFileName,
114+
)
79115

80116
if err := tcmApp.Start(); err != nil {
81117
return err
@@ -90,7 +126,7 @@ func startTcmInteractive() error {
90126
return err
91127
}
92128

93-
log.Printf("(INFO): Interactive process PID %d written to %s\n", tcmApp.Process.Pid, tcmPidFile)
129+
log.Infof("Interactive process PID %d written to %q\n", tcmApp.Process.Pid, tcmPidFile)
94130
return nil
95131
}
96132

@@ -114,7 +150,7 @@ func internalStartTcm(cmdCtx *cmdcontext.CmdCtx, args []string) error {
114150
tcmCtx.Executable = cmdCtx.Cli.TcmCli.Executable
115151

116152
if !tcmCtx.Watchdog {
117-
if err := startTcmInteractive(); err != nil {
153+
if err := startTcmInteractive(tcmCtx.Log.Level); err != nil {
118154
return err
119155
}
120156
} else {
@@ -164,14 +200,32 @@ func internalTcmStop(cmdCtx *cmdcontext.CmdCtx, args []string) error {
164200
if err != nil {
165201
return err
166202
}
167-
log.Println("Watchdog and TCM stopped")
203+
204+
log.Info("Watchdog and TCM stopped")
168205
} else {
169206
_, err := process_utils.StopProcess(tcmPidFile)
170207
if err != nil {
171208
return err
172209
}
173-
log.Println("TCM stopped")
210+
211+
log.Info("TCM stopped")
174212
}
175213

176214
return nil
177215
}
216+
217+
func internalTcmLog(cmdCtx *cmdcontext.CmdCtx, args []string) error {
218+
if tcmCtx.Log.ForceColor {
219+
color.NoColor = false
220+
}
221+
222+
p := tcmCmd.NewLogPrinter(tcmCtx.Log.NoFormat, tcmCtx.Log.NoColor, os.Stdout)
223+
if tcmCtx.Log.IsFollow {
224+
f := tail.NewTailFollower(logFileName)
225+
return tcmCmd.FollowLogs(f, p, tcmCtx.Log.Lines)
226+
}
227+
228+
t := tail.NewTailReader(logFileName)
229+
230+
return tcmCmd.TailLogs(t, p, tcmCtx.Log.Lines)
231+
}

0 commit comments

Comments
 (0)