Skip to content

Commit

Permalink
Merge branch 'master' into staging-server
Browse files Browse the repository at this point in the history
  • Loading branch information
rod-hynes committed May 9, 2023
2 parents 4fe1a3e + e037d81 commit 30068f7
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 25 deletions.
7 changes: 5 additions & 2 deletions psiphon/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,13 @@ type Config struct {
// psinet.Database data.
PsinetDatabaseFilename string

// HostID is the ID of the server host; this is used for API
// event logging.
// HostID identifies the server host; this value is included with all logs.
HostID string

// HostProvider identifies the server host provider; this value is
// included with all logs and logged only when not blank.
HostProvider string

// ServerIPAddress is the public IP address of the server.
ServerIPAddress string

Expand Down
63 changes: 40 additions & 23 deletions psiphon/server/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,20 @@ func (a LogFields) Add(b LogFields) {
}
}

// WithTrace adds a "trace" field containing the caller's function name
// and source file line number; and "host_id" and "build_rev" fields
// WithTrace adds a "trace" field containing the caller's function name and
// source file line number; and "host_id", "provider", and "build_rev" fields
// identifying this server and build. Use this function when the log has no
// fields.
func (logger *TraceLogger) WithTrace() *logrus.Entry {
return logger.WithFields(
logrus.Fields{
"trace": stacktrace.GetParentFunctionName(),
"host_id": logHostID,
"build_rev": logBuildRev,
})
fields := logrus.Fields{
"trace": stacktrace.GetParentFunctionName(),
"host_id": logHostID,
"build_rev": logBuildRev,
}
if logHostProvider != "" {
fields["provider"] = logHostProvider
}
return logger.WithFields(fields)
}

func renameLogFields(fields LogFields) {
Expand All @@ -79,36 +82,50 @@ func renameLogFields(fields LogFields) {
if _, ok := fields["host_id"]; ok {
fields["fields.host_id"] = fields["host_id"]
}
if _, ok := fields["provider"]; ok {
fields["fields.provider"] = fields["provider"]
}
if _, ok := fields["build_rev"]; ok {
fields["fields.build_rev"] = fields["build_rev"]
}
}

// WithTraceFields adds a "trace" field containing the caller's function name
// and source file line number; and "host_id" and "build_rev" fields
// identifying this server and build. Use this function when the log has
// fields. Note that any existing "trace"/"host_id"/"build_rev" field will be
// renamed to "field.<name>".
// and source file line number; and "host_id", "provider", and "build_rev"
// fields identifying this server and build. Use this function when the log
// has fields.
//
// Note that any existing "trace"/"host_id"/"provider"/build_rev" field will
// be renamed to "field.<name>".
func (logger *TraceLogger) WithTraceFields(fields LogFields) *logrus.Entry {
renameLogFields(fields)
fields["trace"] = stacktrace.GetParentFunctionName()
fields["host_id"] = logHostID
if logHostProvider != "" {
fields["provider"] = logHostProvider
}
fields["build_rev"] = logBuildRev
return logger.WithFields(logrus.Fields(fields))
}

// LogRawFieldsWithTimestamp directly logs the supplied fields adding only
// an additional "timestamp" field; and "host_id" and "build_rev" fields
// identifying this server and build. The stock "msg" and "level" fields are
// omitted. This log is emitted at the Error level. This function exists to
// support API logs which have neither a natural message nor severity; and
// omitting these values here makes it easier to ship these logs to existing
// API log consumers.
// Note that any existing "trace"/"host_id"/"build_rev" field will be renamed
// to "field.<name>".
// LogRawFieldsWithTimestamp directly logs the supplied fields adding only an
// additional "timestamp" field; and "host_id", "provider", and "build_rev"
// fields identifying this server and build. The stock "msg" and "level"
// fields are omitted.
//
// This log is emitted at the Error level. This function exists to support API
// logs which have neither a natural message nor severity; and omitting these
// values here makes it easier to ship these logs to existing API log
// consumers.
//
// Note that any existing "trace"/"host_id"/"provider"/"build_rev" field will
// be renamed to "field.<name>".
func (logger *TraceLogger) LogRawFieldsWithTimestamp(fields LogFields) {
renameLogFields(fields)
fields["host_id"] = logHostID
if logHostProvider != "" {
fields["provider"] = logHostProvider
}
fields["build_rev"] = logBuildRev
logger.WithFields(logrus.Fields(fields)).Error(
customJSONFormatterLogRawFieldsWithTimestamp)
Expand Down Expand Up @@ -190,7 +207,6 @@ const customJSONFormatterLogRawFieldsWithTimestamp = "CustomJSONFormatter.LogRaw
// The changes are:
// - "time" is renamed to "timestamp"
// - there's an option to omit the standard "msg" and "level" fields
//
func (f *CustomJSONFormatter) Format(entry *logrus.Entry) ([]byte, error) {
data := make(logrus.Fields, len(entry.Data)+3)
for k, v := range entry.Data {
Expand Down Expand Up @@ -237,7 +253,7 @@ func (f *CustomJSONFormatter) Format(entry *logrus.Entry) ([]byte, error) {
}

var log *TraceLogger
var logHostID, logBuildRev string
var logHostID, logHostProvider, logBuildRev string
var initLogging sync.Once

// InitLogging configures a logger according to the specified
Expand All @@ -252,6 +268,7 @@ func InitLogging(config *Config) (retErr error) {
initLogging.Do(func() {

logHostID = config.HostID
logHostProvider = config.HostProvider
logBuildRev = buildinfo.GetBuildInfo().BuildRev

level, err := logrus.ParseLevel(config.LogLevel)
Expand Down
Loading

0 comments on commit 30068f7

Please sign in to comment.