Skip to content

Commit

Permalink
feat: determine logging mode by environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Jul 5, 2020
1 parent c5547cb commit 2a7977f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
48 changes: 41 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,52 @@ env:

dist: bionic

os:
- linux
- osx
- windows
#os:
# - linux
# - osx
# - windows

go:
#go:
# - 1.9.x
# - 1.10.x
# - 1.11.x
# - 1.12.x
- 1.13.x
- 1.14.x
# - 1.13.x
# - 1.14.x
# - master

matrix:
allow_failures:
- go: master
fast_finish: true
include:
- os: linux
go: 1.13.x
env: GNET_LOGGING_MODE=prod
- os: linux
go: 1.14.x
env: GNET_LOGGING_MODE=prod
- os: linux
go: master
env: GNET_LOGGING_MODE=prod
- os: osx
go: 1.13.x
env: GNET_LOGGING_MODE=dev
- os: osx
go: 1.14.x
env: GNET_LOGGING_MODE=dev
- os: osx
go: master
env: GNET_LOGGING_MODE=dev
- os: windows
go: 1.13.x
env: GNET_LOGGING_MODE=test
- os: windows
go: 1.14.x
env: GNET_LOGGING_MODE=test
- os: windows
go: master
env: GNET_LOGGING_MODE=test

go_import_path: github.com/panjf2000/gnet

Expand Down
4 changes: 2 additions & 2 deletions internal/reuseport/reuseport.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// Package reuseport provides a function that returns fd and net.Listener powered
// by a net.FileListener with a SO_REUSEPORT option set to the socket.
// Package reuseport provides functions that return fd and net.Addr based on
// given the protocol and address with a SO_REUSEPORT option set to the socket.

// +build linux freebsd dragonfly darwin

Expand Down
32 changes: 30 additions & 2 deletions logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,31 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// package logging provides logging functionality for gnet server,
// it sets up a default logger (powered by go.uber.org/zap)
// which is about to be used by gnet server, it also allows users
// to replace the default logger with their customized logger by just
// implementing the `Logger` interface and assign it to the functional option `Options.Logger`,
// pass it to `gnet.Serve` method.
//
// There are two logging modes in zap, instantiated by either NewProduction() or NewDevelopment(),
// the former builds a sensible production Logger that writes InfoLevel and above logs to standard error as JSON,
// it's a shortcut for NewProductionConfig().Build(...Option); the latter builds a development Logger
// that writes DebugLevel and above logs to standard error in a human-friendly format,
// it's a shortcut for NewDevelopmentConfig().Build(...Option).
//
// The environment variable `GNET_LOGGING_MODE` determines which zap logger type will be created for logging,
// "prod" (case-insensitive) means production logger while other values except "prod" including "dev" (case-insensitive)
// represent development logger.

package logging

import "go.uber.org/zap"
import (
"os"
"strings"

"go.uber.org/zap"
)

var (
// DefaultLogger is the default logger inside the gnet server.
Expand All @@ -29,7 +51,13 @@ var (
)

func init() {
zapLogger, _ = zap.NewDevelopment()
switch strings.ToLower(os.Getenv("GNET_LOGGING_MODE")) {
case "prod":
zapLogger, _ = zap.NewProduction()
default:
// Other values except "Prod" create the development logger for gnet server.
zapLogger, _ = zap.NewDevelopment()
}
DefaultLogger = zapLogger.Sugar()
}

Expand Down

0 comments on commit 2a7977f

Please sign in to comment.