Skip to content

Commit 1930111

Browse files
authored
feature(logging): Logging to a file (#813)
This solves #757. JSON Schema has been updated.
1 parent d509bb0 commit 1930111

File tree

9 files changed

+81
-17
lines changed

9 files changed

+81
-17
lines changed

.github/workflows/jsonschema.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: JSON Schema Check
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
json-schema-check:
9+
runs-on: ubuntu-22.04
10+
container: python:3.10-alpine3.16
11+
steps:
12+
- uses: actions/checkout@v3
13+
- name: Test
14+
run: |
15+
pip install jsonschema==4.14.0
16+
for f in $(find config/ -name '*.json')
17+
do
18+
echo "Checking $f"
19+
jsonschema -i $f config-schema.json
20+
done

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
.DS_Store
22
ftpserver
3+
ftpserver.json
4+
ftpserver.log
35
ci-info
46
.idea/
5-
.vscode/
67
*.pem
7-
ftpserver.json
88
gdrive_token_gdrive.json
9-
__debug__bin
9+
__*

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch (log_file)",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "${workspaceFolder}",
13+
"args": ["-conf", "${workspaceFolder}/config/samples/log_file.json"]
14+
}
15+
]
16+
}

config-schema.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@
8686
"type": "object",
8787
"default": {},
8888
"title": "The logging options",
89-
"required": [
90-
"ftp_exchanges",
91-
"file_accesses"
92-
],
9389
"properties": {
9490
"ftp_exchanges": {
9591
"type": "boolean",
@@ -106,11 +102,20 @@
106102
"examples": [
107103
true
108104
]
105+
},
106+
"file": {
107+
"type": "string",
108+
"default": "ftpserver.log",
109+
"title": "Perform logging to a file",
110+
"examples": [
111+
"ftpserver.log"
112+
]
109113
}
110114
},
111115
"examples": [{
112116
"ftp_exchanges": true,
113-
"file_accesses": true
117+
"file_accesses": true,
118+
"file": "ftpserver.log"
114119
}]
115120
},
116121
"tls": {

config/confpar/confpar.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ type PortRange struct {
2828

2929
// Logging defines how we will log accesses
3030
type Logging struct {
31-
FtpExchanges bool `json:"ftp_exchanges"` // Log all ftp exchanges
32-
FileAccesses bool `json:"file_accesses"` // Log all file accesses
31+
FtpExchanges bool `json:"ftp_exchanges"` // Log all ftp exchanges
32+
FileAccesses bool `json:"file_accesses"` // Log all file accesses
33+
File string `json:"file"` // Log file
3334
}
3435

3536
// TLS define the TLS Config

config/samples/log_file.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/fclairamb/ftpserver/main/config-schema.json",
3+
"logging": {
4+
"file": "ftpserver.log"
5+
},
6+
"accesses": []
7+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
require (
2121
cloud.google.com/go/compute v1.7.0 // indirect
2222
github.com/dropbox/dropbox-sdk-go-unofficial v5.6.0+incompatible // indirect
23-
github.com/go-kit/log v0.2.1 // indirect
23+
github.com/go-kit/log v0.2.1
2424
github.com/go-logfmt/logfmt v0.5.1 // indirect
2525
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
2626
github.com/golang/protobuf v1.5.2 // indirect

main.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package main
33

44
import (
55
"flag"
6+
"io"
67
"io/ioutil"
78
"os"
89
"os/signal"
910
"syscall"
1011
"time"
1112

1213
ftpserver "github.com/fclairamb/ftpserverlib"
13-
"github.com/fclairamb/go-log/gokit"
14+
gkwrap "github.com/fclairamb/go-log/gokit"
15+
gokit "github.com/go-kit/log"
1416

1517
"github.com/fclairamb/ftpserver/config"
1618
"github.com/fclairamb/ftpserver/server"
@@ -32,10 +34,7 @@ func main() {
3234
flag.Parse()
3335

3436
// Setting up the logger
35-
logger := gokit.New().With(
36-
"ts", gokit.GKDefaultTimestampUTC,
37-
"caller", gokit.GKDefaultCaller,
38-
)
37+
logger := gkwrap.New()
3938

4039
logger.Info("FTP server", "version", BuildVersion, "date", BuildDate, "commit", Commit)
4140

@@ -52,7 +51,7 @@ func main() {
5251
if _, err := os.Stat(confFile); err != nil && os.IsNotExist(err) {
5352
logger.Warn("No conf file, creating one", "confFile", confFile)
5453

55-
if err := ioutil.WriteFile(confFile, confFileContent(), 0600); err != nil { // nolint: gomnd
54+
if err := ioutil.WriteFile(confFile, confFileContent(), 0600); err != nil { //nolint: gomnd
5655
logger.Warn("Couldn't create conf file", "confFile", confFile)
5756
}
5857
}
@@ -65,6 +64,22 @@ func main() {
6564
return
6665
}
6766

67+
// Now is a good time to open a logging file
68+
if conf.Content.Logging.File != "" {
69+
writer, err := os.OpenFile(conf.Content.Logging.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600) //nolint:gomnd
70+
71+
if err != nil {
72+
logger.Error("Can't open log file", "err", err)
73+
74+
return
75+
}
76+
77+
logger = gkwrap.NewWrap(gokit.NewLogfmtLogger(io.MultiWriter(writer, os.Stdout))).With(
78+
"ts", gokit.DefaultTimestampUTC,
79+
"caller", gokit.DefaultCaller,
80+
)
81+
}
82+
6883
// Loading the driver
6984
var errNewServer error
7085
driver, errNewServer = server.NewServer(conf, logger.With("component", "driver"))

0 commit comments

Comments
 (0)