Skip to main content
Version: 2.8.x(Latest)

For log output with low immediacy requirements, you can output logs asynchronously. Asynchronous output allows logging calls to return immediately, thus being more efficient. glog certainly supports asynchronous output features and uses a goroutine pool internally to manage asynchronous log printing tasks, which can significantly reduce resource usage.

Asynchronous output can be implemented through the SetAsync/ SetFlags methods of the log object or through the chaining operation Async method. However, note that if you set asynchronous output through the object setting methods, all subsequent log outputs will be asynchronous; if you output asynchronously through chaining, only the current log output will be asynchronous.

warning

If both synchronous and asynchronous printing are used for the same file log output, please note the log file content may become disordered, which should be avoided as much as possible.

SetAsync

Let's look at an example of implementing asynchronous printing using the SetAsync method.

package main

import (
"context"
"time"

"github.com/gogf/gf/v2/frame/g"
)

func main() {
ctx := context.TODO()
g.Log().SetAsync(true)
for i := 0; i < 10; i++ {
g.Log().Print(ctx, "async log", i)
}
}

After execution, you can find nothing is output to the terminal because the log output is asynchronous, and the example exits before the log content is output. Therefore, we can make a slight improvement as follows:

package main

import (
"context"
"time"

"github.com/gogf/gf/v2/frame/g"
)

func main() {
ctx := context.TODO()
g.Log().SetAsync(true)
for i := 0; i < 10; i++ {
g.Log().Print(ctx, "async log", i)
}
time.Sleep(time.Second)
}

After execution, the terminal output is:

2019-06-02 15:44:21.399 async log 0
2019-06-02 15:44:21.399 async log 1
2019-06-02 15:44:21.399 async log 2
2019-06-02 15:44:21.399 async log 3
2019-06-02 15:44:21.399 async log 4
2019-06-02 15:44:21.399 async log 5
2019-06-02 15:44:21.399 async log 6
2019-06-02 15:44:21.399 async log 7
2019-06-02 15:44:21.399 async log 8
2019-06-02 15:44:21.399 async log 9

Async Chaining Operation

The chaining operation is relatively simple.

package main

import (
"context"
"time"

"github.com/gogf/gf/v2/frame/g"
)

func main() {
ctx := context.TODO()
for i := 0; i < 10; i++ {
g.Log().Async().Print(ctx, "async log", i)
}
g.Log().Print(ctx, "normal log")
g.Log().Print(ctx, "normal log")
g.Log().Print(ctx, "normal log")
time.Sleep(time.Second)
}

After execution, the terminal output is:

2022-01-05 15:00:44.101 normal log
2022-01-05 15:00:44.101 async log 0
2022-01-05 15:00:44.101 async log 1
2022-01-05 15:00:44.101 async log 2
2022-01-05 15:00:44.101 async log 3
2022-01-05 15:00:44.101 async log 4
2022-01-05 15:00:44.101 async log 5
2022-01-05 15:00:44.101 async log 6
2022-01-05 15:00:44.101 async log 7
2022-01-05 15:00:44.101 async log 8
2022-01-05 15:00:44.101 async log 9
2022-01-05 15:00:44.101 normal log
2022-01-05 15:00:44.103 normal log