diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/config/config.go | 27 | ||||
| -rw-r--r-- | internal/config/defaults.go | 23 | ||||
| -rw-r--r-- | internal/config/flags.go | 1 | ||||
| -rw-r--r-- | internal/config/helpers.gen.go | 25 | ||||
| -rw-r--r-- | internal/log/log.go | 34 | ||||
| -rw-r--r-- | internal/log/syslog_test.go | 16 | 
6 files changed, 90 insertions, 36 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index f1a5bf6e5..86f6b00dd 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -44,19 +44,20 @@ func fieldtag(field, tag string) string {  // will need to regenerate the global Getter/Setter helpers by running:  // `go run ./internal/config/gen/ -out ./internal/config/helpers.gen.go`  type Configuration struct { -	LogLevel        string   `name:"log-level" usage:"Log level to run at: [trace, debug, info, warn, fatal]"` -	LogDbQueries    bool     `name:"log-db-queries" usage:"Log database queries verbosely when log-level is trace or debug"` -	LogClientIP     bool     `name:"log-client-ip" usage:"Include the client IP in logs"` -	ApplicationName string   `name:"application-name" usage:"Name of the application, used in various places internally"` -	LandingPageUser string   `name:"landing-page-user" usage:"the user that should be shown on the instance's landing page"` -	ConfigPath      string   `name:"config-path" usage:"Path to a file containing gotosocial configuration. Values set in this file will be overwritten by values set as env vars or arguments"` -	Host            string   `name:"host" usage:"Hostname to use for the server (eg., example.org, gotosocial.whatever.com). DO NOT change this on a server that's already run!"` -	AccountDomain   string   `name:"account-domain" usage:"Domain to use in account names (eg., example.org, whatever.com). If not set, will default to the setting for host. DO NOT change this on a server that's already run!"` -	Protocol        string   `name:"protocol" usage:"Protocol to use for the REST api of the server (only use http if you are debugging or behind a reverse proxy!)"` -	BindAddress     string   `name:"bind-address" usage:"Bind address to use for the GoToSocial server (eg., 0.0.0.0, 172.138.0.9, [::], localhost). For ipv6, enclose the address in square brackets, eg [2001:db8::fed1]. Default binds to all interfaces."` -	Port            int      `name:"port" usage:"Port to use for GoToSocial. Change this to 443 if you're running the binary directly on the host machine."` -	TrustedProxies  []string `name:"trusted-proxies" usage:"Proxies to trust when parsing x-forwarded headers into real IPs."` -	SoftwareVersion string   `name:"software-version" usage:""` +	LogLevel           string   `name:"log-level" usage:"Log level to run at: [trace, debug, info, warn, fatal]"` +	LogTimestampFormat string   `name:"log-timestamp-format" usage:"Format to use for the log timestamp, as supported by Go's time.Layout"` +	LogDbQueries       bool     `name:"log-db-queries" usage:"Log database queries verbosely when log-level is trace or debug"` +	LogClientIP        bool     `name:"log-client-ip" usage:"Include the client IP in logs"` +	ApplicationName    string   `name:"application-name" usage:"Name of the application, used in various places internally"` +	LandingPageUser    string   `name:"landing-page-user" usage:"the user that should be shown on the instance's landing page"` +	ConfigPath         string   `name:"config-path" usage:"Path to a file containing gotosocial configuration. Values set in this file will be overwritten by values set as env vars or arguments"` +	Host               string   `name:"host" usage:"Hostname to use for the server (eg., example.org, gotosocial.whatever.com). DO NOT change this on a server that's already run!"` +	AccountDomain      string   `name:"account-domain" usage:"Domain to use in account names (eg., example.org, whatever.com). If not set, will default to the setting for host. DO NOT change this on a server that's already run!"` +	Protocol           string   `name:"protocol" usage:"Protocol to use for the REST api of the server (only use http if you are debugging or behind a reverse proxy!)"` +	BindAddress        string   `name:"bind-address" usage:"Bind address to use for the GoToSocial server (eg., 0.0.0.0, 172.138.0.9, [::], localhost). For ipv6, enclose the address in square brackets, eg [2001:db8::fed1]. Default binds to all interfaces."` +	Port               int      `name:"port" usage:"Port to use for GoToSocial. Change this to 443 if you're running the binary directly on the host machine."` +	TrustedProxies     []string `name:"trusted-proxies" usage:"Proxies to trust when parsing x-forwarded headers into real IPs."` +	SoftwareVersion    string   `name:"software-version" usage:""`  	DbType                   string        `name:"db-type" usage:"Database type: eg., postgres"`  	DbAddress                string        `name:"db-address" usage:"Database ipv4 address, hostname, or filename"` diff --git a/internal/config/defaults.go b/internal/config/defaults.go index 61a037157..3703d8372 100644 --- a/internal/config/defaults.go +++ b/internal/config/defaults.go @@ -27,17 +27,18 @@ import (  // Defaults contains a populated Configuration with reasonable defaults. Note that  // if you use this, you will still need to set Host, and, if desired, ConfigPath.  var Defaults = Configuration{ -	LogLevel:        "info", -	LogDbQueries:    false, -	ApplicationName: "gotosocial", -	LandingPageUser: "", -	ConfigPath:      "", -	Host:            "", -	AccountDomain:   "", -	Protocol:        "https", -	BindAddress:     "0.0.0.0", -	Port:            8080, -	TrustedProxies:  []string{"127.0.0.1/32", "::1"}, // localhost +	LogLevel:           "info", +	LogTimestampFormat: "02/01/2006 15:04:05.000", +	LogDbQueries:       false, +	ApplicationName:    "gotosocial", +	LandingPageUser:    "", +	ConfigPath:         "", +	Host:               "", +	AccountDomain:      "", +	Protocol:           "https", +	BindAddress:        "0.0.0.0", +	Port:               8080, +	TrustedProxies:     []string{"127.0.0.1/32", "::1"}, // localhost  	DbType:                   "postgres",  	DbAddress:                "", diff --git a/internal/config/flags.go b/internal/config/flags.go index 386e47293..927f4ddfb 100644 --- a/internal/config/flags.go +++ b/internal/config/flags.go @@ -38,6 +38,7 @@ func (s *ConfigState) AddGlobalFlags(cmd *cobra.Command) {  		cmd.PersistentFlags().String(AccountDomainFlag(), cfg.AccountDomain, fieldtag("AccountDomain", "usage"))  		cmd.PersistentFlags().String(ProtocolFlag(), cfg.Protocol, fieldtag("Protocol", "usage"))  		cmd.PersistentFlags().String(LogLevelFlag(), cfg.LogLevel, fieldtag("LogLevel", "usage")) +		cmd.PersistentFlags().String(LogTimestampFormatFlag(), cfg.LogTimestampFormat, fieldtag("LogTimestampFormat", "usage"))  		cmd.PersistentFlags().Bool(LogDbQueriesFlag(), cfg.LogDbQueries, fieldtag("LogDbQueries", "usage"))  		cmd.PersistentFlags().String(ConfigPathFlag(), cfg.ConfigPath, fieldtag("ConfigPath", "usage")) diff --git a/internal/config/helpers.gen.go b/internal/config/helpers.gen.go index aed111129..ab7d38a8a 100644 --- a/internal/config/helpers.gen.go +++ b/internal/config/helpers.gen.go @@ -49,6 +49,31 @@ func GetLogLevel() string { return global.GetLogLevel() }  // SetLogLevel safely sets the value for global configuration 'LogLevel' field  func SetLogLevel(v string) { global.SetLogLevel(v) } +// GetLogTimestampFormat safely fetches the Configuration value for state's 'LogTimestampFormat' field +func (st *ConfigState) GetLogTimestampFormat() (v string) { +	st.mutex.RLock() +	v = st.config.LogTimestampFormat +	st.mutex.RUnlock() +	return +} + +// SetLogTimestampFormat safely sets the Configuration value for state's 'LogTimestampFormat' field +func (st *ConfigState) SetLogTimestampFormat(v string) { +	st.mutex.Lock() +	defer st.mutex.Unlock() +	st.config.LogTimestampFormat = v +	st.reloadToViper() +} + +// LogTimestampFormatFlag returns the flag name for the 'LogTimestampFormat' field +func LogTimestampFormatFlag() string { return "log-timestamp-format" } + +// GetLogTimestampFormat safely fetches the value for global configuration 'LogTimestampFormat' field +func GetLogTimestampFormat() string { return global.GetLogTimestampFormat() } + +// SetLogTimestampFormat safely sets the value for global configuration 'LogTimestampFormat' field +func SetLogTimestampFormat(v string) { global.SetLogTimestampFormat(v) } +  // GetLogDbQueries safely fetches the Configuration value for state's 'LogDbQueries' field  func (st *ConfigState) GetLogDbQueries() (v bool) {  	st.mutex.RLock() diff --git a/internal/log/log.go b/internal/log/log.go index ae8607fc3..15c917757 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -23,7 +23,6 @@ import (  	"log/syslog"  	"os"  	"strings" -	"sync/atomic"  	"syscall"  	"time" @@ -33,7 +32,7 @@ import (  var (  	// loglvl is the currently set logging level. -	loglvl atomic.Uint32 +	loglvl level.LEVEL  	// lvlstrs is the lookup table of log levels to strings.  	lvlstrs = level.Default() @@ -41,8 +40,9 @@ var (  	// syslog output, only set if enabled.  	sysout *syslog.Writer -	// timefmt is the logging time format used. -	timefmt = "02/01/2006 15:04:05.000" +	// timefmt is the logging time format used, which includes +	// the full field and required quoting +	timefmt = `timestamp="02/01/2006 15:04:05.000" `  	// ctxhooks allows modifying log content based on context.  	ctxhooks []func(context.Context, []kv.Field) []kv.Field @@ -55,12 +55,26 @@ func Hook(hook func(ctx context.Context, kvs []kv.Field) []kv.Field) {  // Level returns the currently set log level.  func Level() level.LEVEL { -	return level.LEVEL(loglvl.Load()) +	return loglvl  }  // SetLevel sets the max logging level.  func SetLevel(lvl level.LEVEL) { -	loglvl.Store(uint32(lvl)) +	loglvl = lvl +} + +// TimeFormat returns the currently-set timestamp format. +func TimeFormat() string { +	return timefmt +} + +// SetTimeFormat sets the timestamp format to the given string. +func SetTimeFormat(format string) { +	if format == "" { +		timefmt = format +		return +	} +	timefmt = `timestamp="` + format + `" `  }  // New starts a new log entry. @@ -164,10 +178,8 @@ func printf(depth int, fields []kv.Field, s string, a ...interface{}) {  	// Acquire buffer  	buf := getBuf() -	// Append formatted timestamp -	buf.B = append(buf.B, `timestamp="`...) +	// Append formatted timestamp according to `timefmt`  	buf.B = time.Now().AppendFormat(buf.B, timefmt) -	buf.B = append(buf.B, `" `...)  	// Append formatted caller func  	buf.B = append(buf.B, `func=`...) @@ -217,10 +229,8 @@ func logf(ctx context.Context, depth int, lvl level.LEVEL, fields []kv.Field, s  	// Acquire buffer  	buf := getBuf() -	// Append formatted timestamp -	buf.B = append(buf.B, `timestamp="`...) +	// Append formatted timestamp according to `timefmt`  	buf.B = time.Now().AppendFormat(buf.B, timefmt) -	buf.B = append(buf.B, `" `...)  	// Append formatted caller func  	buf.B = append(buf.B, `func=`...) diff --git a/internal/log/syslog_test.go b/internal/log/syslog_test.go index e7e82ec58..7432e61b3 100644 --- a/internal/log/syslog_test.go +++ b/internal/log/syslog_test.go @@ -71,6 +71,22 @@ func (suite *SyslogTestSuite) TestSyslog() {  	suite.Regexp(regexp.MustCompile(`timestamp=.* func=.* level=INFO msg="this is a test of the emergency broadcast system!"`), entry["content"])  } +func (suite *SyslogTestSuite) TestSyslogDisableTimestamp() { +	// Get the current format. +	timefmt := log.TimeFormat() + +	// Set an empty timestamp. +	log.SetTimeFormat("") + +	// Set old timestamp on return. +	defer log.SetTimeFormat(timefmt) + +	log.Info(nil, "this is a test of the emergency broadcast system!") + +	entry := <-suite.syslogChannel +	suite.Regexp(regexp.MustCompile(`func=.* level=INFO msg="this is a test of the emergency broadcast system!"`), entry["content"]) +} +  func (suite *SyslogTestSuite) TestSyslogLongMessage() {  	log.Warn(nil, longMessage)  | 
