refactor(init, repo): init logs in fsrepo
Brian Tiger Chow committed
Jan 12, 2015 at 12:41 UTC
c2877b20456dab8b89018c59a4bda393d52f091e
3 files changed
+23
-47
cmd/ipfs/init.go
+15
-43
@@ -4,9 +4,7 @@ import (
4
"bytes"
5
"encoding/base64"
6
"fmt"
7
- "os"
7
"path"
9
- "path/filepath"
8
9
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
10
cmds "github.com/jbenet/go-ipfs/commands"
@@ -18,7 +16,7 @@ import (
16
peer "github.com/jbenet/go-ipfs/p2p/peer"
17
repo "github.com/jbenet/go-ipfs/repo"
18
config "github.com/jbenet/go-ipfs/repo/config"
21
- "github.com/jbenet/go-ipfs/repo/fsrepo"
19
+ fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
20
u "github.com/jbenet/go-ipfs/util"
21
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
22
)
@@ -98,17 +96,19 @@ func doInit(configRoot string, force bool, nBitsForKeypair int) (interface{}, er
96
return nil, err
97
}
98
101
- repo := fsrepo.At(configRoot)
102
- if err := repo.Open(); err != nil {
99
+ r := fsrepo.At(configRoot)
100
+ if err := r.Open(); err != nil {
101
return nil, err
102
}
105
- if err := repo.SetConfig(conf); err != nil {
103
+ if err := r.SetConfig(conf); err != nil {
104
return nil, err
105
}
108
- if err := repo.Close(); err != nil {
106
+ if err := r.Close(); err != nil {
107
+ return nil, err
108
+ }
109
+ if err := repo.ConfigureEventLogger(conf.Logs); err != nil {
110
return nil, err
111
}
111
-
112
err = addTheWelcomeFile(conf)
113
if err != nil {
114
return nil, err
@@ -167,7 +167,7 @@ func initConfig(nBitsForKeypair int) (*config.Config, error) {
167
return nil, err
168
}
169
170
- logConfig, err := initLogs("") // TODO allow user to override dir
170
+ logConfig, err := initLogs()
171
if err != nil {
172
return nil, err
173
}
@@ -191,7 +191,7 @@ func initConfig(nBitsForKeypair int) (*config.Config, error) {
191
192
Bootstrap: bootstrapPeers,
193
Datastore: *ds,
194
- Logs: logConfig,
194
+ Logs: *logConfig,
195
Identity: identity,
196
197
// setup the node mount points.
@@ -240,42 +240,14 @@ func identityConfig(nbits int) (config.Identity, error) {
240
return ident, nil
241
}
242
243
-// initLogs initializes the event logger at the specified path. It uses the
244
-// default log path if no path is provided.
245
-func initLogs(logpath string) (config.Logs, error) {
246
- if len(logpath) == 0 {
247
- var err error
248
- logpath, err = config.LogsPath("")
249
- if err != nil {
250
- return config.Logs{}, debugerror.Wrap(err)
251
- }
252
- }
253
- err := initCheckDir(logpath)
243
+// initLogs initializes the event logger.
244
+func initLogs() (*config.Logs, error) {
245
+ logpath, err := config.LogsPath("")
246
if err != nil {
255
- return config.Logs{}, debugerror.Errorf("logs: %s", err)
247
+ return nil, err
248
}
249
conf := config.Logs{
250
Filename: path.Join(logpath, "events.log"),
251
}
260
- err = repo.ConfigureEventLogger(conf)
261
- if err != nil {
262
- return conf, err
263
- }
264
- return conf, nil
265
-}
266
-
267
-// initCheckDir ensures the directory exists and is writable
268
-func initCheckDir(path string) error {
269
- // Construct the path if missing
270
- if err := os.MkdirAll(path, os.ModePerm); err != nil {
271
- return err
272
- }
273
-
274
- // Check the directory is writeable
275
- if f, err := os.Create(filepath.Join(path, "._check_writeable")); err == nil {
276
- os.Remove(f.Name())
277
- } else {
278
- return debugerror.New("'" + path + "' is not writeable")
279
- }
280
- return nil
252
+ return &conf, nil
253
}
repo/fsrepo/fsrepo.go
+8
@@ -38,6 +38,14 @@ func (r *FSRepo) Open() error {
38
return debugerror.Errorf("datastore: %s", err)
39
}
40
41
+ logpath, err := config.LogsPath("")
42
+ if err != nil {
43
+ return debugerror.Wrap(err)
44
+ }
45
+ if err := initCheckDir(logpath); err != nil {
46
+ return debugerror.Errorf("logs: %s", err)
47
+ }
48
+
49
return nil
50
}
51
repo/logs.go
-4
@@ -7,22 +7,18 @@ import (
7
)
8
9
func ConfigureEventLogger(config config.Logs) error {
10
-
10
if util.Debug {
11
eventlog.Configure(eventlog.LevelDebug)
12
} else {
13
eventlog.Configure(eventlog.LevelInfo)
14
}
16
-
15
eventlog.Configure(eventlog.LdJSONFormatter)
18
-
16
rotateConf := eventlog.LogRotatorConfig{
17
Filename: config.Filename,
18
MaxSizeMB: config.MaxSizeMB,
19
MaxBackups: config.MaxBackups,
20
MaxAgeDays: config.MaxAgeDays,
21
}
25
-
22
eventlog.Configure(eventlog.OutputRotatingLogFile(rotateConf))
23
return nil
24
}