Skip to content
Merged
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
36 changes: 31 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package cmd

import (
"fmt"
"log/syslog"
"os"
"path"
"runtime/pprof"
"strconv"
"strings"
Expand All @@ -16,6 +16,7 @@ import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"gopkg.in/natefinch/lumberjack.v2"
)

var Version = "0.0.0"
Expand All @@ -36,6 +37,11 @@ var serverConfig string
var cpuProfile string
var memProfile string

// The default directory where log files are written.
const logDir = "/var/log/hub/"
const logFileName = "iss2.log"
const globalLogPath = logDir + logFileName

func init() {
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
logInit()
Expand Down Expand Up @@ -71,12 +77,32 @@ func logCallerMarshalFunction(file string, line int) string {
return callerFile + ":" + strconv.Itoa(line)
}

func logInit() {
syslogger, err := syslog.New(syslog.LOG_INFO|syslog.LOG_DEBUG|syslog.LOG_WARNING|syslog.LOG_ERR, "inter-server-sync")
func getFileWriter() *lumberjack.Logger {
logPath := globalLogPath
if file, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0600); err != nil {
homeDir, err := os.UserHomeDir()
if err != nil {
logPath = path.Join(".", logFileName)
} else {
logPath = path.Join(homeDir, logFileName)
}
} else {
file.Close()
}

syslogwriter := zerolog.SyslogLevelWriter(syslogger)
fileLogger := lumberjack.Logger{
Filename: logPath,
MaxSize: 10,
MaxBackups: 5,
MaxAge: 7,
Compress: true,
}
return &fileLogger
}

multi := zerolog.MultiLevelWriter(syslogwriter, os.Stdout)
func logInit() {
fileWriter := getFileWriter()
multi := zerolog.MultiLevelWriter(fileWriter, os.Stdout)
log.Logger = zerolog.New(multi).With().Timestamp().Caller().Logger()
zerolog.CallerMarshalFunc = logCallerMarshalFunction
level, err := zerolog.ParseLevel(logLevel)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/rs/zerolog v1.21.0
github.com/spf13/cobra v1.1.3
github.com/uyuni-project/xmlrpc-public-methods v0.0.0-20200805144514-2ca831c526d1
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
1 change: 1 addition & 0 deletions inter-server-sync.changes.cbosdo.rsyslog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Write log to a rotated file without rsyslog and logrotate
19 changes: 0 additions & 19 deletions inter-server-sync.spec
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ BuildRequires: golang >= 1.18
%else
BuildRequires: golang(API) >= 1.20
%endif
BuildRequires: rsyslog

Requires: gzip
Requires: logrotate
Requires: rsyslog
Requires: systemd
Requires: openssl

Expand All @@ -75,25 +72,12 @@ export GOFLAGS=-mod=vendor
# Add config files for hub
install -d -m 0750 %{buildroot}%{_var}/log/hub

# Add syslog config to redirect logs to /var/log/hub/iss2.log
install -D -m 0644 release/hub-iss-syslogs.conf %{buildroot}%{_sysconfdir}/rsyslog.d/hub-iss.conf

#logrotate config
install -D -m 0644 release/hub-iss-logrotate.conf %{buildroot}%{_sysconfdir}/logrotate.d/inter-server-sync

%check
%if 0%{?rhel}
# Fix OBS debug_package execution.
rm -f %{buildroot}/usr/lib/debug/%{_bindir}/%{name}-%{version}-*.debug
%endif

%post
%if 0%{?rhel}
%systemd_postun rsyslog.service
%else
%service_del_postun rsyslog.service
%endif

%files -f file.lst
%dir %attr(0750, root, root) %{_var}/log/hub

Expand All @@ -102,7 +86,4 @@ rm -f %{buildroot}/usr/lib/debug/%{_bindir}/%{name}-%{version}-*.debug
%license LICENSES
%{_bindir}/inter-server-sync

%config(noreplace) %{_sysconfdir}/rsyslog.d/hub-iss.conf
%config(noreplace) %{_sysconfdir}/logrotate.d/inter-server-sync

%changelog
20 changes: 0 additions & 20 deletions release/hub-iss-logrotate.conf

This file was deleted.

6 changes: 0 additions & 6 deletions release/hub-iss-syslogs.conf

This file was deleted.