Use gitea logging module for git module (#16243)

remove log() func from gogs times and switch to proper logging

Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
6543 2021-06-25 18:54:08 +02:00 committed by GitHub
parent 44b8b07631
commit 3ef23d5411
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 60 additions and 58 deletions

View File

@ -12,6 +12,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"code.gitea.io/gitea/modules/log"
"github.com/djherbis/buffer" "github.com/djherbis/buffer"
"github.com/djherbis/nio/v3" "github.com/djherbis/nio/v3"
) )
@ -99,7 +101,7 @@ func ReadBatchLine(rd *bufio.Reader) (sha []byte, typ string, size int64, err er
} }
idx := strings.IndexByte(typ, ' ') idx := strings.IndexByte(typ, ' ')
if idx < 0 { if idx < 0 {
log("missing space typ: %s", typ) log.Debug("missing space typ: %s", typ)
err = ErrNotExist{ID: string(sha)} err = ErrNotExist{ID: string(sha)}
return return
} }
@ -230,7 +232,7 @@ func ParseTreeLine(rd *bufio.Reader, modeBuf, fnameBuf, shaBuf []byte) (mode, fn
} }
idx := bytes.IndexByte(readBytes, ' ') idx := bytes.IndexByte(readBytes, ' ')
if idx < 0 { if idx < 0 {
log("missing space in readBytes ParseTreeLine: %s", readBytes) log.Debug("missing space in readBytes ParseTreeLine: %s", readBytes)
err = &ErrNotExist{} err = &ErrNotExist{}
return return

View File

@ -12,6 +12,8 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"math" "math"
"code.gitea.io/gitea/modules/log"
) )
// Blob represents a Git object. // Blob represents a Git object.
@ -69,12 +71,12 @@ func (b *Blob) Size() int64 {
defer cancel() defer cancel()
_, err := wr.Write([]byte(b.ID.String() + "\n")) _, err := wr.Write([]byte(b.ID.String() + "\n"))
if err != nil { if err != nil {
log("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err) log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
return 0 return 0
} }
_, _, b.size, err = ReadBatchLine(rd) _, _, b.size, err = ReadBatchLine(rd)
if err != nil { if err != nil {
log("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err) log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
return 0 return 0
} }

View File

@ -15,6 +15,7 @@ import (
"strings" "strings"
"time" "time"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/process"
) )
@ -114,9 +115,9 @@ func (c *Command) RunInDirTimeoutEnvFullPipelineFunc(env []string, timeout time.
} }
if len(dir) == 0 { if len(dir) == 0 {
log(c.String()) log.Debug("%s", c)
} else { } else {
log("%s: %v", dir, c) log.Debug("%s: %v", dir, c)
} }
ctx, cancel := context.WithTimeout(c.parentContext, timeout) ctx, cancel := context.WithTimeout(c.parentContext, timeout)
@ -197,9 +198,8 @@ func (c *Command) RunInDirTimeoutEnv(env []string, timeout time.Duration, dir st
if err := c.RunInDirTimeoutEnvPipeline(env, timeout, dir, stdout, stderr); err != nil { if err := c.RunInDirTimeoutEnvPipeline(env, timeout, dir, stdout, stderr); err != nil {
return nil, ConcatenateError(err, stderr.String()) return nil, ConcatenateError(err, stderr.String())
} }
if stdout.Len() > 0 && log.IsTrace() {
if stdout.Len() > 0 { log.Trace("Stdout:\n %s", stdout.Bytes()[:1024])
log("stdout:\n%s", stdout.Bytes()[:1024])
} }
return stdout.Bytes(), nil return stdout.Bytes(), nil
} }

View File

@ -12,6 +12,8 @@ import (
"io" "io"
"path" "path"
"sort" "sort"
"code.gitea.io/gitea/modules/log"
) )
// GetCommitsInfo gets information of all commits that are corresponding to these entries // GetCommitsInfo gets information of all commits that are corresponding to these entries
@ -78,7 +80,7 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
commitsInfo[i].SubModuleFile = subModuleFile commitsInfo[i].SubModuleFile = subModuleFile
} }
} else { } else {
log("missing commit for %s", entry.Name()) log.Debug("missing commit for %s", entry.Name())
} }
} }

View File

@ -15,6 +15,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/process"
) )
@ -113,7 +114,7 @@ func ParseDiffHunkString(diffhunk string) (leftLine, leftHunk, rightLine, righHu
righHunk, _ = strconv.Atoi(rightRange[1]) righHunk, _ = strconv.Atoi(rightRange[1])
} }
} else { } else {
log("Parse line number failed: %v", diffhunk) log.Debug("Parse line number failed: %v", diffhunk)
rightLine = leftLine rightLine = leftLine
righHunk = leftHunk righHunk = leftHunk
} }

View File

@ -19,9 +19,6 @@ import (
) )
var ( var (
// Debug enables verbose logging on everything.
// This should be false in case Gogs starts in SSH mode.
Debug = false
// Prefix the log prefix // Prefix the log prefix
Prefix = "[git-module] " Prefix = "[git-module] "
// GitVersionRequired is the minimum Git version required // GitVersionRequired is the minimum Git version required
@ -41,19 +38,6 @@ var (
goVersionLessThan115 = true goVersionLessThan115 = true
) )
func log(format string, args ...interface{}) {
if !Debug {
return
}
fmt.Print(Prefix)
if len(args) == 0 {
fmt.Println(format)
} else {
fmt.Printf(format+"\n", args...)
}
}
// LocalVersion returns current Git version from shell. // LocalVersion returns current Git version from shell.
func LocalVersion() (*version.Version, error) { func LocalVersion() (*version.Version, error) {
if err := LoadGitVersion(); err != nil { if err := LoadGitVersion(); err != nil {

View File

@ -9,6 +9,8 @@ import (
"fmt" "fmt"
"os" "os"
"testing" "testing"
"code.gitea.io/gitea/modules/log"
) )
func fatalTestError(fmtStr string, args ...interface{}) { func fatalTestError(fmtStr string, args ...interface{}) {
@ -17,6 +19,8 @@ func fatalTestError(fmtStr string, args ...interface{}) {
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
_ = log.NewLogger(1000, "console", "console", `{"level":"trace","stacktracelevel":"NONE","stderr":true}`)
if err := Init(context.Background()); err != nil { if err := Init(context.Background()); err != nil {
fatalTestError("Init failed: %v", err) fatalTestError("Init failed: %v", err)
} }

View File

@ -1,4 +1,5 @@
// Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -12,6 +13,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
) )
@ -126,11 +128,11 @@ const (
// SetUpdateHook writes given content to update hook of the repository. // SetUpdateHook writes given content to update hook of the repository.
func SetUpdateHook(repoPath, content string) (err error) { func SetUpdateHook(repoPath, content string) (err error) {
log("Setting update hook: %s", repoPath) log.Debug("Setting update hook: %s", repoPath)
hookPath := path.Join(repoPath, HookPathUpdate) hookPath := path.Join(repoPath, HookPathUpdate)
isExist, err := util.IsExist(hookPath) isExist, err := util.IsExist(hookPath)
if err != nil { if err != nil {
log("Unable to check if %s exists. Error: %v", hookPath, err) log.Debug("Unable to check if %s exists. Error: %v", hookPath, err)
return err return err
} }
if isExist { if isExist {

View File

@ -7,6 +7,8 @@ package git
import ( import (
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"code.gitea.io/gitea/modules/log"
) )
// Cache represents a caching interface // Cache represents a caching interface
@ -24,6 +26,6 @@ func (c *LastCommitCache) getCacheKey(repoPath, ref, entryPath string) string {
// Put put the last commit id with commit and entry path // Put put the last commit id with commit and entry path
func (c *LastCommitCache) Put(ref, entryPath, commitID string) error { func (c *LastCommitCache) Put(ref, entryPath, commitID string) error {
log("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID) log.Debug("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
return c.cache.Put(c.getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl()) return c.cache.Put(c.getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl())
} }

View File

@ -10,6 +10,8 @@ import (
"context" "context"
"path" "path"
"code.gitea.io/gitea/modules/log"
"github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/object"
cgobject "github.com/go-git/go-git/v5/plumbing/object/commitgraph" cgobject "github.com/go-git/go-git/v5/plumbing/object/commitgraph"
) )
@ -41,9 +43,9 @@ func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl func() int64,
func (c *LastCommitCache) Get(ref, entryPath string) (interface{}, error) { func (c *LastCommitCache) Get(ref, entryPath string) (interface{}, error) {
v := c.cache.Get(c.getCacheKey(c.repoPath, ref, entryPath)) v := c.cache.Get(c.getCacheKey(c.repoPath, ref, entryPath))
if vs, ok := v.(string); ok { if vs, ok := v.(string); ok {
log("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs) log.Debug("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
if commit, ok := c.commitCache[vs]; ok { if commit, ok := c.commitCache[vs]; ok {
log("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs) log.Debug("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs)
return commit, nil return commit, nil
} }
id, err := c.repo.ConvertToSHA1(vs) id, err := c.repo.ConvertToSHA1(vs)

View File

@ -10,6 +10,8 @@ import (
"bufio" "bufio"
"context" "context"
"path" "path"
"code.gitea.io/gitea/modules/log"
) )
// LastCommitCache represents a cache to store last commit // LastCommitCache represents a cache to store last commit
@ -39,9 +41,9 @@ func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl func() int64,
func (c *LastCommitCache) Get(ref, entryPath string, wr WriteCloserError, rd *bufio.Reader) (interface{}, error) { func (c *LastCommitCache) Get(ref, entryPath string, wr WriteCloserError, rd *bufio.Reader) (interface{}, error) {
v := c.cache.Get(c.getCacheKey(c.repoPath, ref, entryPath)) v := c.cache.Get(c.getCacheKey(c.repoPath, ref, entryPath))
if vs, ok := v.(string); ok { if vs, ok := v.(string); ok {
log("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs) log.Debug("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
if commit, ok := c.commitCache[vs]; ok { if commit, ok := c.commitCache[vs]; ok {
log("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs) log.Debug("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs)
return commit, nil return commit, nil
} }
id, err := c.repo.ConvertToSHA1(vs) id, err := c.repo.ConvertToSHA1(vs)

View File

@ -13,6 +13,8 @@ import (
"io" "io"
"strconv" "strconv"
"strings" "strings"
"code.gitea.io/gitea/modules/log"
) )
// ParseTreeEntries parses the output of a `git ls-tree -l` command. // ParseTreeEntries parses the output of a `git ls-tree -l` command.
@ -120,7 +122,7 @@ loop:
case "40000": case "40000":
entry.entryMode = EntryModeTree entry.entryMode = EntryModeTree
default: default:
log("Unknown mode: %v", string(mode)) log.Debug("Unknown mode: %v", string(mode))
return nil, fmt.Errorf("unknown mode: %v", string(mode)) return nil, fmt.Errorf("unknown mode: %v", string(mode))
} }

View File

@ -12,6 +12,8 @@ import (
"context" "context"
"errors" "errors"
"path/filepath" "path/filepath"
"code.gitea.io/gitea/modules/log"
) )
// Repository represents a Git repository. // Repository represents a Git repository.
@ -54,7 +56,7 @@ func OpenRepository(repoPath string) (*Repository, error) {
// CatFileBatch obtains a CatFileBatch for this repository // CatFileBatch obtains a CatFileBatch for this repository
func (repo *Repository) CatFileBatch() (WriteCloserError, *bufio.Reader, func()) { func (repo *Repository) CatFileBatch() (WriteCloserError, *bufio.Reader, func()) {
if repo.batchCancel == nil || repo.batchReader.Buffered() > 0 { if repo.batchCancel == nil || repo.batchReader.Buffered() > 0 {
log("Opening temporary cat file batch for: %s", repo.Path) log.Debug("Opening temporary cat file batch for: %s", repo.Path)
return CatFileBatch(repo.Path) return CatFileBatch(repo.Path)
} }
return repo.batchWriter, repo.batchReader, func() {} return repo.batchWriter, repo.batchReader, func() {}
@ -63,7 +65,7 @@ func (repo *Repository) CatFileBatch() (WriteCloserError, *bufio.Reader, func())
// CatFileBatchCheck obtains a CatFileBatchCheck for this repository // CatFileBatchCheck obtains a CatFileBatchCheck for this repository
func (repo *Repository) CatFileBatchCheck() (WriteCloserError, *bufio.Reader, func()) { func (repo *Repository) CatFileBatchCheck() (WriteCloserError, *bufio.Reader, func()) {
if repo.checkCancel == nil || repo.checkReader.Buffered() > 0 { if repo.checkCancel == nil || repo.checkReader.Buffered() > 0 {
log("Opening temporary cat file batch-check: %s", repo.Path) log.Debug("Opening temporary cat file batch-check: %s", repo.Path)
return CatFileBatchCheck(repo.Path) return CatFileBatchCheck(repo.Path)
} }
return repo.checkWriter, repo.checkReader, func() {} return repo.checkWriter, repo.checkReader, func() {}

View File

@ -12,6 +12,8 @@ import (
"bytes" "bytes"
"io" "io"
"strings" "strings"
"code.gitea.io/gitea/modules/log"
) )
// IsObjectExist returns true if given reference exists in the repository. // IsObjectExist returns true if given reference exists in the repository.
@ -24,7 +26,7 @@ func (repo *Repository) IsObjectExist(name string) bool {
defer cancel() defer cancel()
_, err := wr.Write([]byte(name + "\n")) _, err := wr.Write([]byte(name + "\n"))
if err != nil { if err != nil {
log("Error writing to CatFileBatchCheck %v", err) log.Debug("Error writing to CatFileBatchCheck %v", err)
return false return false
} }
sha, _, _, err := ReadBatchLine(rd) sha, _, _, err := ReadBatchLine(rd)
@ -41,7 +43,7 @@ func (repo *Repository) IsReferenceExist(name string) bool {
defer cancel() defer cancel()
_, err := wr.Write([]byte(name + "\n")) _, err := wr.Write([]byte(name + "\n"))
if err != nil { if err != nil {
log("Error writing to CatFileBatchCheck %v", err) log.Debug("Error writing to CatFileBatchCheck %v", err)
return false return false
} }
_, _, _, err = ReadBatchLine(rd) _, _, _, err = ReadBatchLine(rd)

View File

@ -12,6 +12,8 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"strings" "strings"
"code.gitea.io/gitea/modules/log"
) )
// ResolveReference resolves a name to a reference // ResolveReference resolves a name to a reference
@ -110,7 +112,7 @@ func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id SHA1) (*Co
return commit, nil return commit, nil
default: default:
log("Unknown typ: %s", typ) log.Debug("Unknown typ: %s", typ)
_, err = rd.Discard(int(size) + 1) _, err = rd.Discard(int(size) + 1)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -13,6 +13,7 @@ import (
"math" "math"
"code.gitea.io/gitea/modules/analyze" "code.gitea.io/gitea/modules/analyze"
"code.gitea.io/gitea/modules/log"
"github.com/go-enry/go-enry/v2" "github.com/go-enry/go-enry/v2"
) )
@ -34,19 +35,19 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
} }
shaBytes, typ, size, err := ReadBatchLine(batchReader) shaBytes, typ, size, err := ReadBatchLine(batchReader)
if typ != "commit" { if typ != "commit" {
log("Unable to get commit for: %s. Err: %v", commitID, err) log.Debug("Unable to get commit for: %s. Err: %v", commitID, err)
return nil, ErrNotExist{commitID, ""} return nil, ErrNotExist{commitID, ""}
} }
sha, err := NewIDFromString(string(shaBytes)) sha, err := NewIDFromString(string(shaBytes))
if err != nil { if err != nil {
log("Unable to get commit for: %s. Err: %v", commitID, err) log.Debug("Unable to get commit for: %s. Err: %v", commitID, err)
return nil, ErrNotExist{commitID, ""} return nil, ErrNotExist{commitID, ""}
} }
commit, err := CommitFromReader(repo, sha, io.LimitReader(batchReader, size)) commit, err := CommitFromReader(repo, sha, io.LimitReader(batchReader, size))
if err != nil { if err != nil {
log("Unable to get commit for: %s. Err: %v", commitID, err) log.Debug("Unable to get commit for: %s. Err: %v", commitID, err)
return nil, err return nil, err
} }
if _, err = batchReader.Discard(1); err != nil { if _, err = batchReader.Discard(1); err != nil {
@ -79,7 +80,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
} }
_, _, size, err := ReadBatchLine(batchReader) _, _, size, err := ReadBatchLine(batchReader)
if err != nil { if err != nil {
log("Error reading blob: %s Err: %v", f.ID.String(), err) log.Debug("Error reading blob: %s Err: %v", f.ID.String(), err)
return nil, err return nil, err
} }

View File

@ -8,6 +8,8 @@ package git
import ( import (
"fmt" "fmt"
"strings" "strings"
"code.gitea.io/gitea/modules/log"
) )
// TagPrefix tags prefix path on the repository // TagPrefix tags prefix path on the repository
@ -33,7 +35,7 @@ func (repo *Repository) CreateAnnotatedTag(name, message, revision string) error
func (repo *Repository) getTag(tagID SHA1, name string) (*Tag, error) { func (repo *Repository) getTag(tagID SHA1, name string) (*Tag, error) {
t, ok := repo.tagCache.Get(tagID.String()) t, ok := repo.tagCache.Get(tagID.String())
if ok { if ok {
log("Hit cache: %s", tagID) log.Debug("Hit cache: %s", tagID)
tagClone := *t.(*Tag) tagClone := *t.(*Tag)
tagClone.Name = name // This is necessary because lightweight tags may have same id tagClone.Name = name // This is necessary because lightweight tags may have same id
return &tagClone, nil return &tagClone, nil

View File

@ -42,16 +42,6 @@ import (
"code.gitea.io/gitea/services/webhook" "code.gitea.io/gitea/services/webhook"
) )
func checkRunMode() {
switch setting.RunMode {
case "dev", "test":
git.Debug = true
default:
git.Debug = false
}
log.Info("Run Mode: %s", strings.Title(setting.RunMode))
}
// NewServices init new services // NewServices init new services
func NewServices() { func NewServices() {
setting.NewServices() setting.NewServices()
@ -84,7 +74,7 @@ func GlobalInit(ctx context.Context) {
log.Trace("AppWorkPath: %s", setting.AppWorkPath) log.Trace("AppWorkPath: %s", setting.AppWorkPath)
log.Trace("Custom path: %s", setting.CustomPath) log.Trace("Custom path: %s", setting.CustomPath)
log.Trace("Log path: %s", setting.LogRootPath) log.Trace("Log path: %s", setting.LogRootPath)
checkRunMode() log.Info("Run Mode: %s", strings.Title(setting.RunMode))
// Setup i18n // Setup i18n
translation.InitLocales() translation.InitLocales()

View File

@ -13,7 +13,6 @@ import (
"testing" "testing"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/highlight" "code.gitea.io/gitea/modules/highlight"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
@ -514,7 +513,6 @@ func TestDiffLine_GetCommentSide(t *testing.T) {
} }
func TestGetDiffRangeWithWhitespaceBehavior(t *testing.T) { func TestGetDiffRangeWithWhitespaceBehavior(t *testing.T) {
git.Debug = true
for _, behavior := range []string{"-w", "--ignore-space-at-eol", "-b", ""} { for _, behavior := range []string{"-w", "--ignore-space-at-eol", "-b", ""} {
diffs, err := GetDiffRangeWithWhitespaceBehavior("./testdata/academic-module", "559c156f8e0178b71cb44355428f24001b08fc68", "bd7063cc7c04689c4d082183d32a604ed27a24f9", diffs, err := GetDiffRangeWithWhitespaceBehavior("./testdata/academic-module", "559c156f8e0178b71cb44355428f24001b08fc68", "bd7063cc7c04689c4d082183d32a604ed27a24f9",
setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles, behavior) setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles, behavior)