Skip to content

Commit

Permalink
Multi-channel and multi-error log support (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
anishnaik authored Aug 22, 2023
1 parent fb09d62 commit 928311f
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 179 deletions.
10 changes: 6 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/crytic/medusa/logging"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"io"
"os"
)

const version = "0.1.1"
Expand All @@ -18,11 +18,13 @@ var rootCmd = &cobra.Command{
}

// cmdLogger is the logger that will be used for the cmd package
var cmdLogger = logging.NewLogger(zerolog.InfoLevel, true, make([]io.Writer, 0)...)
var cmdLogger = logging.NewLogger(zerolog.InfoLevel)

// Execute provides an exportable function to invoke the CLI.
// Returns an error if one was encountered.
// Execute provides an exportable function to invoke the CLI. Returns an error if one was encountered.
func Execute() error {
// Add stdout as an unstructured, colorized output stream for the command logger
cmdLogger.AddWriter(os.Stdout, logging.UNSTRUCTURED, true)

rootCmd.CompletionOptions.DisableDefaultCmd = true
return rootCmd.Execute()
}
14 changes: 6 additions & 8 deletions fuzzing/fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"github.com/crytic/medusa/logging"
"github.com/crytic/medusa/logging/colors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/pkgerrors"
"io"
"math/big"
"math/rand"
"os"
"path/filepath"
"runtime"
"sort"
Expand Down Expand Up @@ -89,12 +88,11 @@ type Fuzzer struct {
// NewFuzzer returns an instance of a new Fuzzer provided a project configuration, or an error if one is encountered
// while initializing the code.
func NewFuzzer(config config.ProjectConfig) (*Fuzzer, error) {
// Create the global logger, set some global logging parameters, and enable terminal coloring
logging.GlobalLogger = logging.NewLogger(config.Logging.Level, true, make([]io.Writer, 0)...)
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
// Create the global logger and add stdout as an unstructured, colored output stream
logging.GlobalLogger = logging.NewLogger(config.Logging.Level)
logging.GlobalLogger.AddWriter(os.Stdout, logging.UNSTRUCTURED, true)

// If the log directory is a non-empty string, create a file for file logging
// If the log directory is a non-empty string, create a file for unstructured, un-colorized file logging
if config.Logging.LogDirectory != "" {
// Filename will be the "log-current_unix_timestamp.log"
filename := "log-" + strconv.FormatInt(time.Now().Unix(), 10) + ".log"
Expand All @@ -104,7 +102,7 @@ func NewFuzzer(config config.ProjectConfig) (*Fuzzer, error) {
logging.GlobalLogger.Error("Failed to create log file", err)
return nil, err
}
logging.GlobalLogger.AddWriter(file, logging.UNSTRUCTURED)
logging.GlobalLogger.AddWriter(file, logging.UNSTRUCTURED, false)
}

// Get the fuzzer's custom sub-logger
Expand Down
16 changes: 16 additions & 0 deletions logging/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package logging

import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/pkgerrors"
)

// init will instantiate the global logger and set up some global parameters from the zerolog package.
func init() {
// Instantiate the global logger
GlobalLogger = NewLogger(zerolog.Disabled)

// Setup stack trace support and set the timestamp format to UNIX
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
}
Loading

0 comments on commit 928311f

Please sign in to comment.