Skip to content

Implement IsTerminal() to set initial CanColorStdout / CanColorStderr in console.go #19469

Closed
@tepozoa

Description

@tepozoa

Feature Description

Currently the values of CanColorStdout and CanColorStderr are set to true. The issue is when running gitea as a systemd unit with logging set to console, it spams the journal / syslog with escape sequences until the code can read the app.ini COLORIZE setting.

Example systemd -> console -> syslog contents that we don't want:

Apr 22 20:42:17 zuul gitea[182204]: #033[36m2022/04/22 20:42:17 #033[0m#033[32mcmd/web.go:102:#033[32mrunWeb()#033[0m #033[1;32
m[I]#033[0m Starting Gitea on PID: #033[1m182204#033[0m

Apr 22 20:42:17 zuul gitea[182204]: #033[36m2022/04/22 20:42:17 #033[0m#033[32mcmd/web.go:150:#033[32mrunWeb()#033[0m #033[1;32
m[I]#033[0m Global init

A golang module exists (and is being imported in go.mod in it's Deprecated form already) to make setting this up a better experience.

The gitea code update could be as simple as:

import "golang.org/x/term"
var CanColorStdout = term.IsTerminal(int(os.Stdout.Fd()))
var CanColorStderr = term.IsTerminal(int(os.Stderr.Fd()))

Screenshots

This can be tested easily outside the codebase:

main.go

package main

import (
	"os"

	"golang.org/x/term"
)

func main() {
	if term.IsTerminal(int(os.Stdout.Fd())) {
		println("STDOUT is a term")
	} else {
		println("STDOUT not a term")
	}
	if term.IsTerminal(int(os.Stderr.Fd())) {
		println("STDERR is a term")
	} else {
		println("STDERR not a term")
	}
}

-> https://go.dev/play/ to see it run and report not a term on their server, then in your local terminals:

$ /usr/lib/go-1.16/bin/go mod init example.com/main
$ /usr/lib/go-1.16/bin/go get golang.org/x/term
$ /usr/lib/go-1.16/bin/go build main.go

$ ./main
STDOUT is a term
STDERR is a term

$ ./main 1>/dev/null
STDOUT not a term
STDERR is a term

$ ./main 2>log.txt; cat log.txt
STDOUT is a term
STDERR not a term

$ ./main 1>/dev/null 2>log.txt; cat log.txt
STDOUT not a term
STDERR not a term

$ ./main 1>log.txt 2>&1; cat log.txt
STDOUT not a term
STDERR not a term

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/featureCompletely new functionality. Can only be merged if feature freeze is not active.type/proposalThe new feature has not been accepted yet but needs to be discussed first.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions