Skip to content

Commit

Permalink
增加配置文件使用案例
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Dec 20, 2023
1 parent 732f275 commit b9c5213
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 4 deletions.
8 changes: 7 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
## ✒ 历史版本的特性介绍 (Features in old versions)

### v1.5.5-alpha (Coming soon)
### v1.5.6-alpha (Coming soon)

> 此版本发布于 2023-12-25
* 完善单元测试,提高覆盖率到 80%

### v1.5.5-alpha

> 此版本发布于 2023-12-20
* 继续优化代码,包括设计和质量
* 完善单元测试,提高覆盖率到 78%

Expand Down
61 changes: 61 additions & 0 deletions _examples/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"encoding/json"
"os"

"github.com/FishGoddess/logit"
"github.com/FishGoddess/logit/extension/config"
)

// newConfig reads config from a json file.
func newConfig() (*config.Config, error) {
conf := new(config.Config)

bs, err := os.ReadFile("config.json")
if err != nil {
return nil, err
}

err = json.Unmarshal(bs, conf)
if err != nil {
return nil, err
}

return conf, err
}

func main() {
// We know you may use a configuration file in your program to setup some resources including logging.
// After thinking about serval kinds of configurations like "yaml", "toml" and "json", we decide to support all of them.
// As you can see, there are many tags on Config's fields like "yaml" and "toml", so you can unmarshal to a config from one of them.
conf, err := newConfig()
if err != nil {
panic(err)
}

opts, err := conf.Options()
if err != nil {
panic(err)
}

// Use options to create a logger.
logger := logit.NewLogger(opts...)
defer logger.Close()

logger.Info("logging from config", "conf", conf)
}
16 changes: 16 additions & 0 deletions _examples/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"level": "debug",
"handler": "tape",
"writer": {
"target": "logit.log",
"file_rotate": true,
"file_max_size": "1GB",
"file_max_age": "7d",
"file_max_backups": 30,
"buffer_size": "64KB",
"batch_size": 16
},
"with_source": false,
"with_pid": true,
"sync_timer": "1s"
}
38 changes: 38 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,44 @@ Package logit provides an easy way to use foundation for your logging operations
defaults.HandleError = func(label string, err error) {
fmt.Printf("%s: %+n\n", label, err)
}
9. config:
// newConfig reads config from a json file.
func newConfig() (*config.Config, error) {
conf := new(config.Config)
bs, err := os.ReadFile("config.json")
if err != nil {
return nil, err
}
err = json.Unmarshal(bs, conf)
if err != nil {
return nil, err
}
return conf, err
}
// We know you may use a configuration file in your program to setup some resources including logging.
// After thinking about serval kinds of configurations like "yaml", "toml" and "json", we decide to support all of them.
// As you can see, there are many tags on Config's fields like "yaml" and "toml", so you can unmarshal to a config from one of them.
conf, err := newConfig()
if err != nil {
panic(err)
}
opts, err := conf.Options()
if err != nil {
panic(err)
}
// Use options to create a logger.
logger := logit.NewLogger(opts...)
defer logger.Close()
logger.Info("logging from config", "conf", conf)
*/
package logit // import "github.com/FishGoddess/logit"

Expand Down
3 changes: 0 additions & 3 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ var (
pid = os.Getpid()
)

// AttrResolver resolves attrs from context.
type AttrResolver = func(ctx context.Context) (attrs []slog.Attr)

// Syncer is an interface that syncs data to somewhere.
type Syncer interface {
Sync() error
Expand Down

0 comments on commit b9c5213

Please sign in to comment.