Skip to content

Commit

Permalink
feature(logging): Logging to a file (#813)
Browse files Browse the repository at this point in the history
This solves #757.

JSON Schema has been updated.
  • Loading branch information
fclairamb authored Sep 23, 2022
1 parent d509bb0 commit 1930111
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 17 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/jsonschema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: JSON Schema Check

on:
push:
pull_request:

jobs:
json-schema-check:
runs-on: ubuntu-22.04
container: python:3.10-alpine3.16
steps:
- uses: actions/checkout@v3
- name: Test
run: |
pip install jsonschema==4.14.0
for f in $(find config/ -name '*.json')
do
echo "Checking $f"
jsonschema -i $f config-schema.json
done
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.DS_Store
ftpserver
ftpserver.json
ftpserver.log
ci-info
.idea/
.vscode/
*.pem
ftpserver.json
gdrive_token_gdrive.json
__debug__bin
__*
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch (log_file)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": ["-conf", "${workspaceFolder}/config/samples/log_file.json"]
}
]
}
15 changes: 10 additions & 5 deletions config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@
"type": "object",
"default": {},
"title": "The logging options",
"required": [
"ftp_exchanges",
"file_accesses"
],
"properties": {
"ftp_exchanges": {
"type": "boolean",
Expand All @@ -106,11 +102,20 @@
"examples": [
true
]
},
"file": {
"type": "string",
"default": "ftpserver.log",
"title": "Perform logging to a file",
"examples": [
"ftpserver.log"
]
}
},
"examples": [{
"ftp_exchanges": true,
"file_accesses": true
"file_accesses": true,
"file": "ftpserver.log"
}]
},
"tls": {
Expand Down
5 changes: 3 additions & 2 deletions config/confpar/confpar.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ type PortRange struct {

// Logging defines how we will log accesses
type Logging struct {
FtpExchanges bool `json:"ftp_exchanges"` // Log all ftp exchanges
FileAccesses bool `json:"file_accesses"` // Log all file accesses
FtpExchanges bool `json:"ftp_exchanges"` // Log all ftp exchanges
FileAccesses bool `json:"file_accesses"` // Log all file accesses
File string `json:"file"` // Log file
}

// TLS define the TLS Config
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions config/samples/log_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/fclairamb/ftpserver/main/config-schema.json",
"logging": {
"file": "ftpserver.log"
},
"accesses": []
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
require (
cloud.google.com/go/compute v1.7.0 // indirect
github.com/dropbox/dropbox-sdk-go-unofficial v5.6.0+incompatible // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-kit/log v0.2.1
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand Down
27 changes: 21 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package main

import (
"flag"
"io"
"io/ioutil"
"os"
"os/signal"
"syscall"
"time"

ftpserver "github.com/fclairamb/ftpserverlib"
"github.com/fclairamb/go-log/gokit"
gkwrap "github.com/fclairamb/go-log/gokit"
gokit "github.com/go-kit/log"

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

// Setting up the logger
logger := gokit.New().With(
"ts", gokit.GKDefaultTimestampUTC,
"caller", gokit.GKDefaultCaller,
)
logger := gkwrap.New()

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

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

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

// Now is a good time to open a logging file
if conf.Content.Logging.File != "" {
writer, err := os.OpenFile(conf.Content.Logging.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600) //nolint:gomnd

if err != nil {
logger.Error("Can't open log file", "err", err)

return
}

logger = gkwrap.NewWrap(gokit.NewLogfmtLogger(io.MultiWriter(writer, os.Stdout))).With(
"ts", gokit.DefaultTimestampUTC,
"caller", gokit.DefaultCaller,
)
}

// Loading the driver
var errNewServer error
driver, errNewServer = server.NewServer(conf, logger.With("component", "driver"))
Expand Down

0 comments on commit 1930111

Please sign in to comment.