Skip to content

Commit

Permalink
Fixed formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
soumeh01 committed Sep 25, 2024
1 parent d4f93d4 commit df4a7b8
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 134 deletions.
176 changes: 88 additions & 88 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
/*
* Copyright (c) 2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
package logger
import (
"fmt"
"io"
"os/exec"
"github.com/sirupsen/logrus"
)
var (
log = New()
)
type LogFormatter struct{}
func (s *LogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
msg := fmt.Sprintf("%s cbuild: %s\n", entry.Level.String(), entry.Message)
return []byte(msg), nil
}
// CustomLogger wraps logrus.Logger and extends the Error method
type CustomLogger struct {
*logrus.Logger
}
func (cl *CustomLogger) Format(entry *logrus.Entry) ([]byte, error) {
msg := fmt.Sprintf("%s cbuild: %s\n", entry.Level.String(), entry.Message)
return []byte(msg), nil
}
// New creates a new instance of CustomLogger
func New() *CustomLogger {
logger := &CustomLogger{logrus.StandardLogger()}
logger.SetFormatter(new(LogFormatter))
return logger
}
// Error method overrides logrus.Error with additional custom logic
func Error(args ...interface{}) {
for _, arg := range args {
switch arg.(type) {
case *exec.ExitError:
logrus.Info(arg)
default:
logrus.Error(arg)
}
}
}
// Wrapping the standard functions
func Info(args ...interface{}) {
log.Info(args...)
}
func Warn(args ...interface{}) {
log.Warn(args...)
}
func Debug(args ...interface{}) {
log.Debug(args...)
}
func SetLevel(level logrus.Level) {
log.SetLevel(level)
}
func GetLevel() logrus.Level {
return log.GetLevel()
}
func SetFormatter(formatter logrus.Formatter) {
log.SetFormatter(formatter)
}
func SetOutput(out io.Writer) {
log.SetOutput(out)
}
func StandardLogger() *logrus.Logger {
return logrus.StandardLogger()
}
/*
* Copyright (c) 2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

package logger

import (
"fmt"
"io"
"os/exec"

"github.com/sirupsen/logrus"
)

var (
log = New()
)

type LogFormatter struct{}

func (s *LogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
msg := fmt.Sprintf("%s cbuild: %s\n", entry.Level.String(), entry.Message)
return []byte(msg), nil
}

// CustomLogger wraps logrus.Logger and extends the Error method
type CustomLogger struct {
*logrus.Logger
}

func (cl *CustomLogger) Format(entry *logrus.Entry) ([]byte, error) {
msg := fmt.Sprintf("%s cbuild: %s\n", entry.Level.String(), entry.Message)
return []byte(msg), nil
}

// New creates a new instance of CustomLogger
func New() *CustomLogger {
logger := &CustomLogger{logrus.StandardLogger()}
logger.SetFormatter(new(LogFormatter))
return logger
}

// Error method overrides logrus.Error with additional custom logic
func Error(args ...interface{}) {
for _, arg := range args {
switch arg.(type) {
case *exec.ExitError:
logrus.Info(arg)
default:
logrus.Error(arg)
}
}
}

// Wrapping the standard functions
func Info(args ...interface{}) {
log.Info(args...)
}

func Warn(args ...interface{}) {
log.Warn(args...)
}

func Debug(args ...interface{}) {
log.Debug(args...)
}

func SetLevel(level logrus.Level) {
log.SetLevel(level)
}

func GetLevel() logrus.Level {
return log.GetLevel()
}

func SetFormatter(formatter logrus.Formatter) {
log.SetFormatter(formatter)
}

func SetOutput(out io.Writer) {
log.SetOutput(out)
}

func StandardLogger() *logrus.Logger {
return logrus.StandardLogger()
}
92 changes: 46 additions & 46 deletions pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
/*
* Copyright (c) 2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
package logger
import (
"bytes"
"errors"
"fmt"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCustomErrorMethod(t *testing.T) {
// Set up buffer to capture log output
var logOutput bytes.Buffer
SetOutput(&logOutput)
// Simulate an exec.ExitError
cmd := exec.Command("cmd", "/C", "exit", "1")
err := cmd.Run()
// Log the error using the custom Error method
if exitErr, ok := err.(*exec.ExitError); ok {
Error(exitErr)
}
aa := logOutput.String()
fmt.Println(aa)
// Assert that custom logic was applied by checking the log output for "exit_code"
assert.Contains(t, aa, "info cbuild: exit status 1\n")
// Clear the buffer
logOutput.Reset()
// Test logging a generic error
genericErr := errors.New("generic error")
Error(genericErr)
// Assert that the original logrus Error behavior was called
assert.Contains(t, logOutput.String(), "generic error", "Expected log output to contain 'generic error'")
}
/*
* Copyright (c) 2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

package logger

import (
"bytes"
"errors"
"fmt"
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCustomErrorMethod(t *testing.T) {
// Set up buffer to capture log output
var logOutput bytes.Buffer
SetOutput(&logOutput)

// Simulate an exec.ExitError
cmd := exec.Command("cmd", "/C", "exit", "1")
err := cmd.Run()

// Log the error using the custom Error method
if exitErr, ok := err.(*exec.ExitError); ok {
Error(exitErr)
}

aa := logOutput.String()
fmt.Println(aa)
// Assert that custom logic was applied by checking the log output for "exit_code"
assert.Contains(t, aa, "info cbuild: exit status 1\n")

// Clear the buffer
logOutput.Reset()

// Test logging a generic error
genericErr := errors.New("generic error")
Error(genericErr)
// Assert that the original logrus Error behavior was called
assert.Contains(t, logOutput.String(), "generic error", "Expected log output to contain 'generic error'")
}

0 comments on commit df4a7b8

Please sign in to comment.