Skip to content

Commit

Permalink
Merge branch 'main' into optimize-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerogers authored Sep 29, 2023
2 parents dbffacf + 83b80c7 commit 6ec5fdc
Show file tree
Hide file tree
Showing 16 changed files with 1,050 additions and 165 deletions.
32 changes: 31 additions & 1 deletion content/en/docs/hertz/getting-started/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,18 @@ For more information on how to use hz, please refer to: [hz](https://www.cloudwe
### Generate/Complete the Sample Code

1. Create the hertz_demo folder in the current directory and go to that directory.
2. Generate code `hz new`. If your codes are not placed under `GOPATH`, you need to refer [here](https://www.cloudwego.io/docs/hertz/tutorials/toolkit/usage/) and add `-module` (or `-mod`) flag to name your custom module.
2. Generating code
- Use `hz new` directly, if not currently in `GOPATH`, you need to add `-module` or `-mod` flag to specify a custom module name. See [here](https://www.cloudwego.io/docs/hertz/tutorials/toolkit/usage/) for details.
- Code generation by specifying an already defined idl file, e.g. `hz new -idl hello.thrift`.
```thrift
namespace go hello.world
service HelloService {
string Hello(1: string name);
}
```
After execution, a scaffolding of the Hertz project is created in the current directory, with a `ping` interface for testing.
3. Tidy & get dependencies.
```bash
Expand Down Expand Up @@ -148,6 +159,25 @@ If nothing goes wrong, we can see the following output:

You have now successfully launched Hertz Server successfully and completed an API call.

### Updating project code

If you need to make further updates to the project, you should use the `hz update` command, here is an example of adding a `Bye` method.

```thrift
namespace go hello.world
service HelloService {
string Hello(1: string name);
string Bye(1: string name);
}
```

At this point, run `hz update` from the project root directory to update the project.

```bash
hz update -idl hello.thrift
```

## More examples

Please refer:[Example code](/docs/hertz/tutorials/example/)
32 changes: 29 additions & 3 deletions content/en/docs/hertz/tutorials/framework-exten/log/logrus.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: "Hertz interfaces with logrus and lumberjack."

---

## Logger
## Logger structure

```go
var _ hlog.FullLogger = (*Logger)(nil)
Expand All @@ -22,7 +22,7 @@ type Logger struct {
## NewLogger

`NewLogger` uses `defaultConfig()` to create and initialize a Logger. The required configuration can be passed into the function as a parameter. If no parameter is passed in, the initial configuration will be installed to create `Logger`
For related configuration, please refer to "option configuration" below.
For related configuration, please refer to [option configuration](#option-configuration) below

Function Signature:

Expand All @@ -45,6 +45,32 @@ func main() {
}
```

## Logger

`Logger` is used to return an instance of `*logrus.Logger` for custom fields, etc

Function Signature:

```go
func (l *Logger) Logger() *logrus.Logger
```

Sample code:

```go
package main

import (
hertzlogrus "github.com/hertz-contrib/logger/logrus"
"github.com/sirupsen/logrus"
)

func main() {
logger := hertzlogrus.NewLogger(hertzlogrus.WithLogger(logrus.New()))
l := logger.Logger()
}
```

## Option configuration

### WithLogger
Expand Down Expand Up @@ -173,4 +199,4 @@ func main() {
}
```

For more details on how to adapt the interface of hlog, see [hertz-contrib/logger/logrus](https://github.com/hertz-contrib/logger/tree/main/logrus)
For more details on how to adapt the interface of hlog, see [hertz-contrib/logger/logrus](https://github.com/hertz-contrib/logger/tree/main/logrus)
233 changes: 233 additions & 0 deletions content/en/docs/hertz/tutorials/framework-exten/log/slog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
---
title: "slog"
linkTitle: "slog"
weight: 5
keywords: ["Logger Extension", "slog"]
description: "Hertz interfaces with slog and lumberjack."

---

## Logger structure

```go
var _ hlog.FullLogger = (*Logger)(nil)

type Logger struct {
l *slog.Logger
cfg *config
}
```

## NewLogger

Create and initialize a Logger through `defaultConfig()`. The required configuration can be passed into the function as a parameter. If no parameters are passed in, the initial configuration will be installed to create the Logger.
For related configuration, please refer to [option configuration](#option-configuration) below

Currently only `slog.NewJSONHandler()` in the slog library is supported, `slog.NewTextHandler()` is not supported

Function Signature:

```go
func NewLogger(opts ...Option) *Logger
```

Sample code:

```go
package main

import (
"github.com/cloudwego/hertz/pkg/common/hlog"
hertzslog "github.com/hertz-contrib/logger/slog"
"os"
)

func main() {
logger := hertzslog.NewLogger(hertzslog.WithOutput(os.Stdout))
hlog.SetLogger(logger)
}
```

## Logger

`Logger` is used to obtain `*slog.Logger` to meet complex operations

Function Signature:

```go
func (l *Logger) Logger() *slog.Logger
```

Sample code:

```go
package main

import (
hertzslog "github.com/hertz-contrib/logger/slog"
"os"
)

func main() {
logger := hertzslog.NewLogger(hertzslog.WithOutput(os.Stdout))
l := logger.Logger()
}
```

## Option configuration

### WithOutput

`WithOutput` is used to set the output location of the log

Function Signature:

```go
func WithOutput(writer io.Writer) Option
```

Sample code:

```go
package main

import (
hertzslog "github.com/hertz-contrib/logger/slog"
"os"
)

func main() {
logger := hertzslog.NewLogger(hertzslog.WithOutput(os.Stdout))
}


```

### WithLevel

`WithLevel` judges the incoming `*slog.LevelVar`. Only log levels higher than or equal to this will be recorded

>It is worth noting that if `WithLevel` is set together with `WithHandlerOptions`, the log level of WithLevel will override the log level in WithHandlerOptions
Function Signature:

```go
func WithLevel(lvl *slog.LevelVar) Option
```

Sample code:

```go
package main

import (
hertzslog "github.com/hertz-contrib/logger/slog"
)

func main() {
//Empty LevelVar corresponds to LevelInfo
logger := hertzslog.NewLogger(hertzslog.WithLevel(&slog.LevelVar{}))

//Dynamically set the log level to Level Debug
levelVar := slog.LevelVar{}
levelVar.Set(slog.LevelDebug)
logger := hertzslog.NewLogger(hertzslog.WithLevel(&slog.LevelVar{}))
}

```

### WithHandlerOptions

`WithHandlerOptions` passes `*slog.HandlerOptions` into the configuration

Function Signature:

```go
func WithHandlerOptions(opts *slog.HandlerOptions) Option
```

Sample code:

```go
package main

import (
hertzslog "github.com/hertz-contrib/logger/slog"
)

func main() {
logger := hertzslog.NewLogger(hertzslog.WithHandlerOptions(&slog.HandlerOptions{
AddSource: false,
Level: slog.LevelInfo,
ReplaceAttr: nil,
}))
}
```

## A complete slog example

```go
package main

import (
"context"
"log"
"os"
"path"
"time"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/protocol/consts"
hertzslog "github.com/hertz-contrib/logger/slog"
"gopkg.in/natefinch/lumberjack.v2"
)

func main() {
h := server.Default()

// Customizable output directory.
var logFilePath string
dir := "./hlog"
logFilePath = dir + "/logs/"
if err := os.MkdirAll(logFilePath, 0o777); err != nil {
log.Println(err.Error())
return
}

// 将文件名设置为日期
logFileName := time.Now().Format("2006-01-02") + ".log"
fileName := path.Join(logFilePath, logFileName)
if _, err := os.Stat(fileName); err != nil {
if _, err := os.Create(fileName); err != nil {
log.Println(err.Error())
return
}
}

logger := hertzslog.NewLogger()
// set filename to date
lumberjackLogger := &lumberjack.Logger{
Filename: fileName,
MaxSize: 20, // A file can be up to 20M.
MaxBackups: 5, // Save up to 5 files at the same time
MaxAge: 10, // A file can be saved for up to 10 days.
Compress: true, // Compress with gzip.
}

logger.SetOutput(lumberjackLogger)
logger.SetLevel(hlog.LevelDebug)

hlog.SetLogger(logger)

h.GET("/hello", func(ctx context.Context, c *app.RequestContext) {
hlog.Info("Hello, hertz")
c.String(consts.StatusOK, "Hello hertz!")
})

h.Spin()
}
```

For more details on how to adapt the interface of hlog, see [hertz-contrib/logger/slog](https://github.com/hertz-contrib/logger/tree/main/slog)
Loading

0 comments on commit 6ec5fdc

Please sign in to comment.