refactor(eventlog) integrate into fsrepo
now, eventlogger works wherever the fsrepo is used
Brian Tiger Chow committed
Jan 24, 2015 at 01:20 UTC
98ad33e005d75e63ed0c337bf6d1c9a6cdf0efaf
7 files changed
+67
-85
cmd/ipfs/init.go
-23
@@ -4,7 +4,6 @@ import (
4
"bytes"
5
"encoding/base64"
6
"fmt"
7
- "path"
7
8
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9
cmds "github.com/jbenet/go-ipfs/commands"
@@ -14,7 +13,6 @@ import (
13
ipns "github.com/jbenet/go-ipfs/fuse/ipns"
14
ci "github.com/jbenet/go-ipfs/p2p/crypto"
15
peer "github.com/jbenet/go-ipfs/p2p/peer"
17
- repo "github.com/jbenet/go-ipfs/repo"
16
config "github.com/jbenet/go-ipfs/repo/config"
17
fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
18
u "github.com/jbenet/go-ipfs/util"
@@ -110,9 +108,6 @@ func doInit(repoRoot string, force bool, nBitsForKeypair int) (interface{}, erro
108
if err := fsrepo.Init(repoRoot, conf); err != nil {
109
return nil, err
110
}
113
- if err := repo.ConfigureEventLogger(conf.Logs); err != nil {
114
- return nil, err
115
- }
111
err = addTheWelcomeFile(repoRoot)
112
if err != nil {
113
return nil, err
@@ -196,11 +191,6 @@ func initConfig(nBitsForKeypair int) (*config.Config, error) {
191
return nil, err
192
}
193
199
- logConfig, err := initLogs()
200
- if err != nil {
201
- return nil, err
202
- }
203
-
194
bootstrapPeers, err := corecmds.DefaultBootstrapPeers()
195
if err != nil {
196
return nil, err
@@ -220,7 +210,6 @@ func initConfig(nBitsForKeypair int) (*config.Config, error) {
210
211
Bootstrap: bootstrapPeers,
212
Datastore: *ds,
223
- Logs: *logConfig,
213
Identity: identity,
214
215
// setup the node mount points.
@@ -268,15 +257,3 @@ func identityConfig(nbits int) (config.Identity, error) {
257
fmt.Printf("peer identity: %s\n", ident.PeerID)
258
return ident, nil
259
}
271
-
272
-// initLogs initializes the event logger.
273
-func initLogs() (*config.Logs, error) {
274
- logpath, err := config.LogsPath("")
275
- if err != nil {
276
- return nil, err
277
- }
278
- conf := config.Logs{
279
- Filename: path.Join(logpath, "events.log"),
280
- }
281
- return &conf, nil
282
-}
cmd/ipfs/main.go
-12
@@ -20,7 +20,6 @@ import (
20
cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
21
cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
22
core "github.com/jbenet/go-ipfs/core"
23
- repo "github.com/jbenet/go-ipfs/repo"
23
config "github.com/jbenet/go-ipfs/repo/config"
24
fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
25
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
@@ -282,17 +281,6 @@ func callPreCommandHooks(ctx context.Context, details cmdDetails, req cmds.Reque
281
}
282
}
283
285
- // When the upcoming command may use the config and repo, we know it's safe
286
- // for the log config hook to touch the config/repo
287
- if repo.IsInitialized(req.Context().ConfigRoot) {
288
- log.Debug("Calling hook: Configure Event Logger")
289
- cfg, err := req.Context().GetConfig()
290
- if err != nil {
291
- return err
292
- }
293
- repo.ConfigureEventLogger(cfg.Logs)
294
- }
295
-
284
return nil
285
}
286
repo/config/config.go
-1
@@ -23,7 +23,6 @@ type Config struct {
23
Version Version // local node's version management
24
Bootstrap []BootstrapPeer // local nodes's bootstrap peers
25
Tour Tour // local node's tour position
26
- Logs Logs // local node's event log configuration
26
}
27
28
const (
repo/config/logs.go
-17
@@ -1,18 +1 @@
1
package config
2
-
3
-// LogsDefaultDirectory is the directory to store all IPFS event logs.
4
-var LogsDefaultDirectory = "logs"
5
-
6
-// Logs tracks the configuration of the event logger
7
-type Logs struct {
8
- Filename string
9
- MaxSizeMB uint64
10
- MaxBackups uint64
11
- MaxAgeDays uint64
12
-}
13
-
14
-// LogsPath returns the default path for event logs given a configuration root
15
-// (set an empty string to have the default configuration root)
16
-func LogsPath(configroot string) (string, error) {
17
- return Path(configroot, LogsDefaultDirectory)
18
-}
repo/fsrepo/component/eventlog.go
new
+51
@@ -0,0 +1,51 @@
1
+package component
2
+
3
+import (
4
+ "os"
5
+ "path"
6
+
7
+ config "github.com/jbenet/go-ipfs/repo/config"
8
+ dir "github.com/jbenet/go-ipfs/thirdparty/dir"
9
+ eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
10
+)
11
+
12
+func InitEventlogComponent(repoPath string, conf *config.Config) error {
13
+ if err := dir.Writable(path.Join(repoPath, "logs")); err != nil {
14
+ return err
15
+ }
16
+ return nil
17
+}
18
+
19
+func EventlogComponentIsInitialized(path string) bool {
20
+ return true
21
+}
22
+
23
+type EventlogComponent struct {
24
+ path string
25
+}
26
+
27
+func (c *EventlogComponent) SetPath(path string) {
28
+ c.path = path // FIXME necessary?
29
+}
30
+
31
+func (c *EventlogComponent) Close() error {
32
+ // TODO It isn't part of the current contract, but callers may like for us
33
+ // to disable logging once the component is closed.
34
+ eventlog.Configure(eventlog.Output(os.Stderr))
35
+ return nil
36
+}
37
+
38
+func (c *EventlogComponent) Open() error {
39
+ // log.Debugf("writing eventlogs to ...", c.path)
40
+ return configureEventLoggerAtRepoPath(c.path)
41
+}
42
+
43
+func configureEventLoggerAtRepoPath(repoPath string) error {
44
+ eventlog.Configure(eventlog.LevelInfo)
45
+ eventlog.Configure(eventlog.LdJSONFormatter)
46
+ rotateConf := eventlog.LogRotatorConfig{
47
+ Filename: path.Join(repoPath, "logs", "events.log"),
48
+ }
49
+ eventlog.Configure(eventlog.OutputRotatingLogFile(rotateConf))
50
+ return nil
51
+}
repo/fsrepo/fsrepo.go
+16
-8
@@ -54,6 +54,7 @@ type FSRepo struct {
54
// TODO test
55
configComponent component.ConfigComponent
56
datastoreComponent component.DatastoreComponent
57
+ eventlogComponent component.EventlogComponent
58
}
59
60
type componentBuilder struct {
@@ -163,14 +164,6 @@ func (r *FSRepo) Open() error {
164
}
165
}
166
166
- logpath, err := config.LogsPath("")
167
- if err != nil {
168
- return debugerror.Wrap(err)
169
- }
170
- if err := dir.Writable(logpath); err != nil {
171
- return debugerror.Errorf("logs: %s", err)
172
- }
173
-
167
return r.transitionToOpened()
168
}
169
@@ -352,5 +345,20 @@ func componentBuilders() []componentBuilder {
345
return nil
346
},
347
},
348
+
349
+ // EventlogComponent
350
+ componentBuilder{
351
+ Init: component.InitEventlogComponent,
352
+ IsInitialized: component.EventlogComponentIsInitialized,
353
+ OpenHandler: func(r *FSRepo) error {
354
+ c := component.EventlogComponent{}
355
+ c.SetPath(r.path)
356
+ if err := c.Open(); err != nil {
357
+ return err
358
+ }
359
+ r.eventlogComponent = c
360
+ return nil
361
+ },
362
+ },
363
}
364
}
repo/logs.go
deleted
-24
@@ -1,24 +0,0 @@
1
-package repo
2
-
3
-import (
4
- config "github.com/jbenet/go-ipfs/repo/config"
5
- eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
6
- util "github.com/jbenet/go-ipfs/util"
7
-)
8
-
9
-func ConfigureEventLogger(config config.Logs) error {
10
- if util.Debug {
11
- eventlog.Configure(eventlog.LevelDebug)
12
- } else {
13
- eventlog.Configure(eventlog.LevelInfo)
14
- }
15
- eventlog.Configure(eventlog.LdJSONFormatter)
16
- rotateConf := eventlog.LogRotatorConfig{
17
- Filename: config.Filename,
18
- MaxSizeMB: config.MaxSizeMB,
19
- MaxBackups: config.MaxBackups,
20
- MaxAgeDays: config.MaxAgeDays,
21
- }
22
- eventlog.Configure(eventlog.OutputRotatingLogFile(rotateConf))
23
- return nil
24
-}