-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
134 additions
and
134 deletions.
There are no files selected for viewing
This file contains 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,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() | ||
} |
This file contains 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,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'") | ||
} |