Skip to content

Commit 1cf0cbf

Browse files
authored
Merge pull request #141 from arangodb-helper/custom-log-dir
Added `--log.dir` for custom log file folder
2 parents b3c19ae + f91c446 commit 1cf0cbf

13 files changed

+395
-28
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Changes from version 0.10.4 to master
22

3+
- Added `--log.dir` option to configure a custom directory to which all log files will be written.
4+
- It is no longer allowed to use `log.file` as a passthrough option.
35
- Added `--starter.host` option, to bind the HTTP server to a specific network interface instead of the default `0.0.0.0`.
46
- Added `POST /data-auto-upgrade` support to perform a rolling upgrade of all servers (with single `--database.auto-upgrade` restart)
57
- Renamed mode option `resilientsingle` to `activefailover`. (`resilientsingle` is being supported as alias for a while)

docs/Manual/Programs/Starter/Options.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,14 @@ debugging only.
277277

278278
show more information (default false).
279279

280+
- `--log.dir=path`
281+
282+
set a custom directory to which all log files will be written to.
283+
When using the Starter in docker, make sure that this directory is
284+
mounted as a volume for the Starter.
285+
286+
Note: When using a custom log directory, all files will be named as `arangod-<role>-<port>.log`.
287+
280288
- `--log.rotate-files-to-keep=int`
281289

282290
set the number of old log files to keep when rotating log files of server components (default 5).

main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ var (
9696
startLocalSlaves bool
9797
mode string
9898
dataDir string
99+
logDir string // Custom log directory (default "")
99100
ownAddress string
100101
bindAddress string
101102
masterAddresses []string
@@ -159,6 +160,7 @@ func init() {
159160
f.BoolVar(&enableSync, "starter.sync", false, "If set, the starter will also start arangosync instances")
160161

161162
pf.BoolVar(&verbose, "log.verbose", false, "Turn on debug logging")
163+
pf.StringVar(&logDir, "log.dir", getEnvVar("LOG_DIR", ""), "Custom log file directory.")
162164
f.IntVar(&logRotateFilesToKeep, "log.rotate-files-to-keep", defaultLogRotateFilesToKeep, "Number of files to keep when rotating log files")
163165
f.DurationVar(&logRotateInterval, "log.rotate-interval", defaultLogRotateInterval, "Time between log rotations (0 disables log rotation)")
164166

@@ -545,6 +547,14 @@ func mustPrepareService(generateAutoKeyFile bool) (*service.Service, service.Boo
545547
log.Fatalf("Cannot create data directory %s because %v, giving up.", dataDir, err)
546548
}
547549

550+
// Make custom log directory absolute
551+
if logDir != "" {
552+
logDir, _ = filepath.Abs(logDir)
553+
if err := os.MkdirAll(logDir, 0755); err != nil {
554+
log.Fatalf("Cannot create custom log directory %s because %v, giving up.", logDir, err)
555+
}
556+
}
557+
548558
// Read jwtSecret (if any)
549559
var jwtSecret string
550560
if jwtSecretFile != "" {
@@ -651,6 +661,7 @@ func mustPrepareService(generateAutoKeyFile bool) (*service.Service, service.Boo
651661
MasterPort: masterPort,
652662
RrPath: rrPath,
653663
DataDir: dataDir,
664+
LogDir: logDir,
654665
OwnAddress: ownAddress,
655666
BindAddress: bindAddress,
656667
MasterAddresses: masterAddresses,

service/arangod_config_builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func createArangodConf(log *logging.Logger, bsCfg BootstrapConfig, myHostDir, my
129129
}
130130

131131
// createArangodArgs returns the command line arguments needed to run an arangod server of given type.
132-
func createArangodArgs(log *logging.Logger, config Config, clusterConfig ClusterConfig, myContainerDir string,
132+
func createArangodArgs(log *logging.Logger, config Config, clusterConfig ClusterConfig, myContainerDir, myContainerLogFile string,
133133
myPeerID, myAddress, myPort string, serverType ServerType, arangodConfig configFile, agentRecoveryID string, databaseAutoUpgrade bool) []string {
134134
containerConfFileName := filepath.Join(myContainerDir, arangodConfFileName)
135135

@@ -149,7 +149,7 @@ func createArangodArgs(log *logging.Logger, config Config, clusterConfig Cluster
149149
optionPair{"--database.directory", slasher(filepath.Join(myContainerDir, "data"))},
150150
optionPair{"--javascript.startup-directory", slasher(jsStartup)},
151151
optionPair{"--javascript.app-path", slasher(filepath.Join(myContainerDir, "apps"))},
152-
optionPair{"--log.file", slasher(filepath.Join(myContainerDir, arangodLogFileName))},
152+
optionPair{"--log.file", slasher(myContainerLogFile)},
153153
optionPair{"--log.force-direct", "false"},
154154
)
155155
if databaseAutoUpgrade {

service/arangosync_config_builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func createArangoSyncClusterSecretFile(log *logging.Logger, bsCfg BootstrapConfi
7171
}
7272

7373
// createArangoSyncArgs returns the command line arguments needed to run an arangosync server of given type.
74-
func createArangoSyncArgs(log *logging.Logger, config Config, clusterConfig ClusterConfig, myContainerDir string,
74+
func createArangoSyncArgs(log *logging.Logger, config Config, clusterConfig ClusterConfig, myContainerDir, myContainerLogFile string,
7575
myPeerID, myAddress, myPort string, serverType ServerType, clusterJWTSecretFile string) ([]string, error) {
7676

7777
options := make([]optionPair, 0, 32)
@@ -81,7 +81,7 @@ func createArangoSyncArgs(log *logging.Logger, config Config, clusterConfig Clus
8181
}
8282

8383
options = append(options,
84-
optionPair{"--log.file", filepath.Join(myContainerDir, arangoSyncLogFileName)},
84+
optionPair{"--log.file", myContainerLogFile},
8585
optionPair{"--server.endpoint", "https://" + net.JoinHostPort(myAddress, myPort)},
8686
optionPair{"--server.port", myPort},
8787
optionPair{"--monitoring.token", config.SyncMonitoringToken},

service/passthrough.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var (
5252
"database.directory",
5353
"javascript.startup-directory",
5454
"javascript.app-path",
55+
"log.file",
5556
"rocksdb.encryption-keyfile",
5657
"server.endpoint",
5758
"server.authentication",

service/process_type.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
package service
2424

25+
import "fmt"
26+
2527
// ProcessType specifies the types of process (the starter can work with).
2628
type ProcessType string
2729

@@ -49,12 +51,12 @@ func (s ProcessType) CommandFileName() string {
4951
}
5052

5153
// LogFileName returns the name of the log file used by this process
52-
func (s ProcessType) LogFileName() string {
54+
func (s ProcessType) LogFileName(suffix string) string {
5355
switch s {
5456
case ProcessTypeArangod:
55-
return arangodLogFileName
57+
return fmt.Sprintf("arangod%s.log", suffix)
5658
case ProcessTypeArangoSync:
57-
return arangoSyncLogFileName
59+
return fmt.Sprintf("arangosync%s.log", suffix)
5860
default:
5961
return ""
6062
}

service/runtime_server_manager.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ type runtimeServerManagerContext interface {
5959

6060
// serverHostDir returns the path of the folder (in host namespace) containing data for the given server.
6161
serverHostDir(serverType ServerType) (string, error)
62+
// serverContainerDir returns the path of the folder (in container namespace) containing data for the given server.
63+
serverContainerDir(serverType ServerType) (string, error)
64+
65+
// serverHostLogFile returns the path of the logfile (in host namespace) to which the given server will write its logs.
66+
serverHostLogFile(serverType ServerType) (string, error)
67+
// serverContainerLogFile returns the path of the logfile (in container namespace) to which the given server will write its logs.
68+
serverContainerLogFile(serverType ServerType) (string, error)
6269

6370
// removeRecoveryFile removes any recorded RECOVERY file.
6471
removeRecoveryFile()
@@ -88,6 +95,15 @@ func startServer(ctx context.Context, log *logging.Logger, runtimeContext runtim
8895
if err != nil {
8996
return nil, false, maskAny(err)
9097
}
98+
myContainerDir, err := runtimeContext.serverContainerDir(serverType)
99+
if err != nil {
100+
return nil, false, maskAny(err)
101+
}
102+
myContainerLogFile, err := runtimeContext.serverContainerLogFile(serverType)
103+
if err != nil {
104+
return nil, false, maskAny(err)
105+
}
106+
91107
os.MkdirAll(filepath.Join(myHostDir, "data"), 0755)
92108
os.MkdirAll(filepath.Join(myHostDir, "apps"), 0755)
93109

@@ -120,7 +136,6 @@ func startServer(ctx context.Context, log *logging.Logger, runtimeContext runtim
120136
}
121137

122138
log.Infof("Starting %s on port %d", serverType, myPort)
123-
myContainerDir := runner.GetContainerDir(myHostDir, dockerDataDir)
124139
processType := serverType.ProcessType()
125140
// Create/read arangod.conf
126141
var confVolumes []Volume
@@ -147,7 +162,7 @@ func startServer(ctx context.Context, log *logging.Logger, runtimeContext runtim
147162
clusterConfig, myPeer, _ := runtimeContext.ClusterConfig()
148163
upgradeManager := runtimeContext.UpgradeManager()
149164
databaseAutoUpgrade := upgradeManager.ServerDatabaseAutoUpgrade(serverType)
150-
args, err := createServerArgs(log, config, clusterConfig, myContainerDir, myPeer.ID, myHostAddress, strconv.Itoa(myPort), serverType, arangodConfig, containerSecretFileName, bsCfg.RecoveryAgentID, databaseAutoUpgrade)
165+
args, err := createServerArgs(log, config, clusterConfig, myContainerDir, myContainerLogFile, myPeer.ID, myHostAddress, strconv.Itoa(myPort), serverType, arangodConfig, containerSecretFileName, bsCfg.RecoveryAgentID, databaseAutoUpgrade)
151166
if err != nil {
152167
return nil, false, maskAny(err)
153168
}
@@ -174,13 +189,11 @@ func startServer(ctx context.Context, log *logging.Logger, runtimeContext runtim
174189

175190
// showRecentLogs dumps the most recent log lines of the server of given type to the console.
176191
func (s *runtimeServerManager) showRecentLogs(log *logging.Logger, runtimeContext runtimeServerManagerContext, serverType ServerType) {
177-
myHostDir, err := runtimeContext.serverHostDir(serverType)
192+
logPath, err := runtimeContext.serverHostLogFile(serverType)
178193
if err != nil {
179-
log.Errorf("Cannot find server host dir: %#v", err)
194+
log.Errorf("Cannot find server host log file: %#v", err)
180195
return
181196
}
182-
logFileName := serverType.ProcessType().LogFileName()
183-
logPath := filepath.Join(myHostDir, logFileName)
184197
logFile, err := os.Open(logPath)
185198
if os.IsNotExist(err) {
186199
log.Infof("Log file for %s is empty", serverType)
@@ -363,13 +376,11 @@ func (s *runtimeServerManager) rotateLogFile(ctx context.Context, log *logging.L
363376
}
364377

365378
// Prepare log path
366-
myHostDir, err := runtimeContext.serverHostDir(serverType)
379+
logPath, err := runtimeContext.serverHostLogFile(serverType)
367380
if err != nil {
368-
log.Debugf("Failed to get host directory for '%s': %s", serverType, err)
381+
log.Debugf("Failed to get host log file for '%s': %s", serverType, err)
369382
return
370383
}
371-
logFileName := serverType.ProcessType().LogFileName()
372-
logPath := filepath.Join(myHostDir, logFileName)
373384
log.Debugf("Rotating %s log file: %s", serverType, logPath)
374385

375386
// Move old files

service/server.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"net/http"
3333
"net/url"
3434
"os"
35-
"path/filepath"
3635
"strconv"
3736

3837
"github.com/arangodb-helper/arangodb/client"
@@ -85,8 +84,8 @@ type httpServerContext interface {
8584
// IsRunningMaster returns if the starter is the running master.
8685
IsRunningMaster() (isRunningMaster, isRunning bool, masterURL string)
8786

88-
// serverHostDir returns the path of the folder (in host namespace) containing data for the given server.
89-
serverHostDir(serverType ServerType) (string, error)
87+
// serverHostLogFile returns the path of the logfile (in host namespace) to which the given server will write its logs.
88+
serverHostLogFile(serverType ServerType) (string, error)
9089

9190
// sendMasterLeaveCluster informs the master that we're leaving for good.
9291
// The master will remove the database servers from the cluster and update
@@ -496,14 +495,12 @@ func (s *httpServer) syncWorkerLogsHandler(w http.ResponseWriter, r *http.Reques
496495

497496
func (s *httpServer) logsHandler(w http.ResponseWriter, r *http.Request, serverType ServerType) {
498497
// Find log path
499-
myHostDir, err := s.context.serverHostDir(serverType)
498+
logPath, err := s.context.serverHostLogFile(serverType)
500499
if err != nil {
501500
// Not ready yet
502501
w.WriteHeader(http.StatusServiceUnavailable)
503502
return
504503
}
505-
logFileName := serverType.ProcessType().LogFileName()
506-
logPath := filepath.Join(myHostDir, logFileName)
507504
s.log.Debugf("Fetching logs in %s", logPath)
508505
rd, err := os.Open(logPath)
509506
if os.IsNotExist(err) {

service/server_config_builder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ func collectServerConfigVolumes(serverType ServerType, config configFile) []Volu
7373
}
7474

7575
// createServerArgs returns the command line arguments needed to run an arangod/arangosync server of given type.
76-
func createServerArgs(log *logging.Logger, config Config, clusterConfig ClusterConfig, myContainerDir string,
76+
func createServerArgs(log *logging.Logger, config Config, clusterConfig ClusterConfig, myContainerDir, myContainerLogFile string,
7777
myPeerID, myAddress, myPort string, serverType ServerType, arangodConfig configFile,
7878
clusterJWTSecretFile, agentRecoveryID string, databaseAutoUpgrade bool) ([]string, error) {
7979
switch serverType.ProcessType() {
8080
case ProcessTypeArangod:
81-
return createArangodArgs(log, config, clusterConfig, myContainerDir, myPeerID, myAddress, myPort, serverType, arangodConfig, agentRecoveryID, databaseAutoUpgrade), nil
81+
return createArangodArgs(log, config, clusterConfig, myContainerDir, myContainerLogFile, myPeerID, myAddress, myPort, serverType, arangodConfig, agentRecoveryID, databaseAutoUpgrade), nil
8282
case ProcessTypeArangoSync:
83-
return createArangoSyncArgs(log, config, clusterConfig, myContainerDir, myPeerID, myAddress, myPort, serverType, clusterJWTSecretFile)
83+
return createArangoSyncArgs(log, config, clusterConfig, myContainerDir, myContainerLogFile, myPeerID, myAddress, myPort, serverType, clusterJWTSecretFile)
8484
default:
8585
return nil, nil
8686
}

0 commit comments

Comments
 (0)