-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Allow global logging when developing #2671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,64 @@ | ||
package logs | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
|
||
"github.com/aybabtme/humanlog" | ||
"github.com/jesseduffield/lazygit/pkg/config" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// TailLogs lets us run `lazygit --logs` to print the logs produced by other lazygit processes. | ||
// This makes for easier debugging. | ||
func TailLogs() { | ||
logFilePath, err := config.LogPath() | ||
if err != nil { | ||
log.Fatal(err) | ||
// It's important that this package does not depend on any other package because we | ||
// may want to import it from anywhere, and we don't want to create a circular dependency | ||
// (because Go refuses to compile circular dependencies). | ||
|
||
// Global is a global logger that can be used anywhere in the app, for | ||
// _development purposes only_. I want to avoid global variables when possible, | ||
// so if you want to log something that's printed when the -debug flag is set, | ||
// you'll need to ensure the struct you're working with has a logger field ( | ||
// and most of them do). | ||
// Global is only available if the LAZYGIT_LOG_PATH environment variable is set. | ||
var Global *logrus.Entry | ||
|
||
func init() { | ||
logPath := os.Getenv("LAZYGIT_LOG_PATH") | ||
if logPath != "" { | ||
Global = NewDevelopmentLogger(logPath) | ||
} | ||
} | ||
|
||
fmt.Printf("Tailing log file %s\n\n", logFilePath) | ||
func NewProductionLogger() *logrus.Entry { | ||
logger := logrus.New() | ||
logger.Out = io.Discard | ||
logger.SetLevel(logrus.ErrorLevel) | ||
return formatted(logger) | ||
} | ||
|
||
opts := humanlog.DefaultOptions | ||
opts.Truncates = false | ||
func NewDevelopmentLogger(logPath string) *logrus.Entry { | ||
logger := logrus.New() | ||
logger.SetLevel(getLogLevel()) | ||
|
||
_, err = os.Stat(logFilePath) | ||
file, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o666) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
log.Fatal("Log file does not exist. Run `lazygit --debug` first to create the log file") | ||
} | ||
log.Fatal(err) | ||
log.Fatalf("Unable to log to log file: %v", err) | ||
} | ||
logger.SetOutput(file) | ||
return formatted(logger) | ||
} | ||
|
||
TailLogsForPlatform(logFilePath, opts) | ||
func formatted(log *logrus.Logger) *logrus.Entry { | ||
// highly recommended: tail -f development.log | humanlog | ||
// https://github.com/aybabtme/humanlog | ||
log.Formatter = &logrus.JSONFormatter{} | ||
|
||
return log.WithFields(logrus.Fields{}) | ||
} | ||
|
||
func getLogLevel() logrus.Level { | ||
strLevel := os.Getenv("LOG_LEVEL") | ||
level, err := logrus.ParseLevel(strLevel) | ||
if err != nil { | ||
return logrus.DebugLevel | ||
} | ||
return level | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package tail | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/aybabtme/humanlog" | ||
) | ||
|
||
// TailLogs lets us run `lazygit --logs` to print the logs produced by other lazygit processes. | ||
// This makes for easier debugging. | ||
func TailLogs(logFilePath string) { | ||
fmt.Printf("Tailing log file %s\n\n", logFilePath) | ||
|
||
opts := humanlog.DefaultOptions | ||
opts.Truncates = false | ||
|
||
_, err := os.Stat(logFilePath) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
log.Fatal("Log file does not exist. Run `lazygit --debug` first to create the log file") | ||
} | ||
log.Fatal(err) | ||
} | ||
|
||
tailLogsForPlatform(logFilePath, opts) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved this into its own package to ensure we have as few dependencies as possible in the logs package