msgpack代替json防止丢失类型
nats-server系统服务只能使用sc命令注册

zap日志写入通道被覆盖

妙音 posted @ 2021年8月17日 20:00 in golang , 266 阅读
 

描述

 
实现了一个io.Writer接口,允许zap日志写入,传递给界面,出现了日志被覆盖、重复的情况。
 

原因

 
io.Writer的接口Write传入的[]byte真实类型是slice。通道传递slice时,用的是引用传递,所以真实数据有被覆盖的情况 
 

示例

 
* zap初始化
 
...
out = COut()
zapcore.NewCore(encoder, zapcore.AddSync(out), clevel),
...
 
* 自定义日志写入Channel
 
package logs

var _GlobalChanOutput = &ChanOutput{
outs: make([]chan *[]byte, 0),
}

/*---------------------------------------
全局函数
示例
logs.COut().AddOut(xx)
---------------------------------------*/

//全局ChanOutput
func COut() *ChanOutput{
return _GlobalChanOutput
}

/*---------------------------------------
log to channel
---------------------------------------*/

type ChanOutput struct{
outs []chan *[]byte
}

//io.Writer接口
func (c *ChanOutput) Write(p []byte) (n int, err error) {
count :=0

//数据复制(防止被覆盖)
_p := make([]byte, len(p))
copy(_p, p)

for _, out :=range c.outs{
select{
case out <- &_p:
count += 1
default:
}
}
return count, nil
}

func (c *ChanOutput) AddOut(out chan *[]byte) {
c.outs = append(c.outs, out)
}
 
* 读取日志
 
logOut := make(chan *[]byte, 1024)
logs.COut().AddOut(logOut)

pctx, pctxCancel := context.WithCancel(context.Background())
go func(ctx context.Context){
    select {
    case msg := <- logOut:
        fmt.Println(string(*msg))
    case <-ctx.Done():
        break
    }
}(pctx)
 
 
Avatar_small
danske casino 说:
2021年8月21日 23:24

It all is wholly right. All of little components were definitely generated thru countless checklist learning. I enjoy your application tons.

Avatar_small
AAA 说:
2021年9月03日 00:29

I'm glad I found this web site, I couldn't find any knowledge on this matter prior to.Also operate a site and if you are ever interested in doing some visitor writing for me if possible feel free to let me know, im always look for people to check out my web site. магазин интернет магазин

Avatar_small
dfg` 说:
2021年9月04日 23:58

I have been checking out a few of your stories and i can state pretty good stuff. I will definitely bookmark your blog Instagram++

Avatar_small
sites for followers 说:
2021年9月06日 16:06

Everything is entirely correct. All of little parts were certainly created through innumerable agenda learning. I partake in your application tons.

Avatar_small
sites for followers 说:
2021年9月06日 18:14

I have been looking at a couple of your accounts and I can state very great stuff. I will bookmark your blog.

Avatar_small
sites for followers 说:
2021年9月06日 18:14

I have been taking a gander at two or three your records and I can state extremely extraordinary stuff. I will bookmark your blog

Avatar_small
tonny 说:
2021年9月06日 19:50

its good for programer who famoused in social media feilds and its alos usefull for beginers

Avatar_small
john 说:
2021年9月06日 20:06

thanks for sharing this useful codes

Avatar_small
max 说:
2021年9月06日 20:17

its cool i try this its very useful

Avatar_small
dfg` 说:
2021年9月12日 22:15

Love to read it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks. https://barowatt.com

Avatar_small
dfg` 说:
2021年9月13日 19:13

Thank you very much for this great post. free promo codes to play doubledown

Avatar_small
dfg` 说:
2021年9月15日 17:23

Cool stuff you have got and you keep update all of us. HD Streamz

Avatar_small
dfg` 说:
2021年9月17日 15:50

There are specific dissertation web sites over the web any time you access not surprisingly launched in the web blog. Crypto Mining

Avatar_small
Hardik 说:
2021年9月17日 17:55

I’m impressed, I must say. Truly rarely must i encounter a weblog that’s both educative and entertaining, and without a doubt, you have hit the nail about the head. Your concept is outstanding; the thing is something too few people are speaking intelligently about. My business is happy that we stumbled across this within my search for some thing relating to this.

Avatar_small
dfg` 说:
2021年9月19日 18:35

Pretty good post. I have just stumbled upon your blog and enjoyed reading your blog posts very much. I am looking for new posts to get more precious info. Big thanks for the useful info. pubg mobile download hack

Avatar_small
dfg` 说:
2021年9月21日 22:31

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. blackpods

Avatar_small
dfg` 说:
2021年9月24日 15:11

We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work. Garden Paradise Seeds

Avatar_small
dfg` 说:
2021年9月25日 00:36

Dengue fever is another mosquito-borne deadly disease mostly finds in tropical areas. A huge number of people from India, Southeast Asia, China, the Caribbean island, and African tested Dengue Symptoms every year.

Avatar_small
dfg` 说:
2021年9月27日 00:21

Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Sofa cleaning Havant

Avatar_small
dfg` 说:
2021年9月27日 18:00

A great content material as well as great layout. Your website deserves all of the positive feedback it’s been getting. I will be back soon for further quality contents. idn poker

Avatar_small
dfg` 说:
2021年9月28日 01:08

This content is written very well. Your use of formatting when making your points makes your observations very clear and easy to understand. Thank you. moving-local-distance-to-austin

Avatar_small
dfg` 说:
2021年9月28日 01:14

Thanks for every other informative site. The place else may just I get that kind of information written in such an ideal means? I have a venture that I’m just now operating on, and I have been on the look out for such information. Packing Tips: The Complete List of Dos And Don’ts For Long-Distance Moving

Avatar_small
dfg` 说:
2021年9月28日 01:20

I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work. What Is the Average Cost of Moving an Apartment?

Avatar_small
dfg` 说:
2021年9月28日 18:15

This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work. Free Business ListingThis is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work. Free Business Listing

Avatar_small
huram 说:
2021年9月28日 22:54

I should assert barely that its astounding! The blog is informational also always fabricate amazing entitys. <a href="https://www.thenewcanadians.com/?fbclid=IwAR3_6ZAviXa01yzNp94ajApHUvZANDZL3ENoZO1NTM26P0DbmDHzOqTs7eg">situs idn poker</a>

Avatar_small
dfg` 说:
2021年10月01日 20:36

Thank you for such a well written article. It’s full of insightful information and entertaining descriptions. Your point of view is the best among many. pikashow apk

Avatar_small
dfg` 说:
2021年10月05日 18:36

Efficiently written information. It will be profitable to anybody who utilizes it, counting me. Keep up the good work. For certain I will review out more posts day in and day out. Blackpods Pro

Avatar_small
dfg` 说:
2021年10月07日 15:12

I visit your blog regularly and recommend it to all of those who wanted to enhance their knowledge with ease. The style of writing is excellent and also the content is top-notch. Thanks for that shrewdness you provide the readers! crowdfunding software real estate for sale

Avatar_small
dfg` 说:
2021年10月10日 22:09

Someone Sometimes with visits your blog regularly and recommended it in my experience to read as well. The way of writing is excellent and also the content is top-notch. Thanks for that insight you provide the readers! endreinigung

Avatar_small
dfg` 说:
2021年10月12日 18:09

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work! 먹튀폴리스

Avatar_small
dfg` 说:
2021年10月14日 13:59

Excellent blog! I found it while surfing around on Google. Content of this page is unique as well as well researched. Appreciate it. Latest rap news

Avatar_small
dfg` 说:
2021年10月16日 22:13

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me. 핀벳88검증


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter