Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ go:
- 1.4
- 1.5
- tip

install:
- go get -v "github.com/smartystreets/goconvey"
- go get -v "github.com/op/go-logging"
- go get -v "github.com/sirupsen/logrus"

script:
- go test -run="TestJustToTest"
95 changes: 93 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Point of this library is to fully implement FreeSWITCH ESL and bring outbound se

Before using this code, please read following discussion: https://github.com/0x19/goesl/discussions/35

In short, I don't have time right now contributing to this project.
In short, I don't have time right now contributing to this project.


### TODO?
Expand All @@ -28,8 +28,99 @@ There are few different types of examples that can be found at [GoESL Examples](

Feel free to suggest more examples :)

### Logging

### Contributions / Issues?
GoESL now uses [logrus](https://github.com/sirupsen/logrus) for structured logging. The underlying logrus logger is exposed directly as `goesl.Logger`, giving you full access to all logrus functionality including atomic operations, structured logging with fields, hooks, and advanced configuration.

#### Direct Logger Access

The most powerful way to configure logging is to use the exposed logger directly:

```go
import (
"github.com/0x19/goesl"
"github.com/sirupsen/logrus"
)

// Set log level using logrus types (thread-safe with built-in atomics)
goesl.Logger.SetLevel(logrus.DebugLevel)

// Use any logrus formatter
goesl.Logger.SetFormatter(&logrus.JSONFormatter{})

// Add structured logging with fields
goesl.Logger.WithFields(logrus.Fields{
"user_id": 123,
"action": "login",
}).Info("User logged in")

// Use context-aware logging
goesl.Logger.WithField("component", "auth").Warn("Authentication failed")
```

#### Convenience Functions (Backward Compatibility)

For backward compatibility, the original logging functions are still available:

```go
goesl.Debug("Debug message")
goesl.Info("Info message")
goesl.Warning("Warning message")
goesl.Error("Error message")
goesl.Fatal("Fatal message")
goesl.Panic("Panic message")
goesl.Trace("Trace message")
```

#### Environment Variables

You can also configure logging via environment variables:

```bash
# Set log level (panic, fatal, error, warn, info, debug, trace)
export GOESL_LOG_LEVEL=debug

# Set log format (text or json)
export GOESL_LOG_FORMAT=json
```

#### Advanced Usage Examples

```go
package main

import (
"github.com/0x19/goesl"
"github.com/sirupsen/logrus"
)

func main() {
// Configure logger directly with full logrus power
goesl.Logger.SetLevel(logrus.TraceLevel)
goesl.Logger.SetFormatter(&logrus.JSONFormatter{
PrettyPrint: true,
})

// Structured logging
logger := goesl.Logger.WithFields(logrus.Fields{
"service": "freeswitch-client",
"version": "1.0.0",
})

logger.Info("Service starting")

// Context-aware logging
goesl.Logger.WithField("call_id", "123").Debug("Processing call")

// Use all logrus features: hooks, custom formatters, etc.
// Check current level
if goesl.Logger.IsLevelEnabled(logrus.DebugLevel) {
goesl.Logger.Debug("Debug logging is enabled")
}
}
```

This approach gives you the full power of logrus while maintaining backward compatibility with the original GoESL logging functions.### Contributions / Issues?

Please reach me over [email protected] or visit my [website](http://www.neviovesic.com/) or submit new [issue](https://github.com/0x19/goesl/issues/new). I'd prefer tho if you would submit [issue](https://github.com/0x19/goesl/issues/new).

Expand Down
72 changes: 55 additions & 17 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,80 @@ package goesl
import (
"os"

"github.com/op/go-logging"
"github.com/sirupsen/logrus"
)

var (
log = logging.MustGetLogger("goesl")

// Example format string. Everything except the message has a custom color
// which is dependent on the log level. Many fields have a custom output
// formatting too, eg. the time returns the hour down to the milli second.
format = logging.MustStringFormatter(
"%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.8s}%{color:reset} %{message}",
)
// Logger exposes the underlying logrus logger for direct configuration
// Use Logger.SetLevel(logrus.DebugLevel), Logger.SetFormatter(), etc.
Logger = logrus.New()
)

// Convenience functions for backward compatibility
func Debug(message string, args ...interface{}) {
log.Debugf(message, args...)
Logger.Debugf(message, args...)
}

func Error(message string, args ...interface{}) {
log.Errorf(message, args...)
Logger.Errorf(message, args...)
}

func Notice(message string, args ...interface{}) {
log.Noticef(message, args...)
Logger.Infof(message, args...)
}

func Info(message string, args ...interface{}) {
log.Infof(message, args...)
Logger.Infof(message, args...)
}

func Warning(message string, args ...interface{}) {
log.Warningf(message, args...)
Logger.Warnf(message, args...)
}

func Warn(message string, args ...interface{}) {
Logger.Warnf(message, args...)
}

func Fatal(message string, args ...interface{}) {
Logger.Fatalf(message, args...)
}

func Panic(message string, args ...interface{}) {
Logger.Panicf(message, args...)
}

func Trace(message string, args ...interface{}) {
Logger.Tracef(message, args...)
}

func init() {
backend := logging.NewLogBackend(os.Stderr, "", 0)
formatter := logging.NewBackendFormatter(backend, format)
logging.SetBackend(formatter)
// Set default configuration
Logger.SetOutput(os.Stderr)
Logger.SetLevel(logrus.InfoLevel)
Logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "15:04:05.000",
})

// Check for environment variable to set log level
if level := os.Getenv("GOESL_LOG_LEVEL"); level != "" {
if logLevel, err := logrus.ParseLevel(level); err == nil {
Logger.SetLevel(logLevel)
} else {
Logger.Warnf("Invalid log level '%s', defaulting to info: %v", level, err)
}
}

// Check for environment variable to set log format
if format := os.Getenv("GOESL_LOG_FORMAT"); format != "" {
switch format {
case "json":
Logger.SetFormatter(&logrus.JSONFormatter{})
case "text":
Logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "15:04:05.000",
})
}
}
}