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

zap日志写入通道被覆盖

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

描述

 
实现了一个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
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月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月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月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月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
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
AAA 说:
2021年12月23日 16:58

Many thanks for sharing this great post. Very interesting ideas! (as always, btw) 안전놀이터

Avatar_small
AAA 说:
2022年2月17日 14:20

well, if you really want to be healthy, i believe that veggan foods are the best ; 메이저사이트

Avatar_small
AAA 说:
2022年2月23日 21:56

being an entrepreneur opened up lots of business leads on my line of work, i like to make money both online and offline,, Bitcoin Merchant Services

 

==============================================

 

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. North American Bancard ISO Program

 

==============================================

 

I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... payment processing agent

Avatar_small
AAA 说:
2022年3月04日 23:36

I really like your article. It’s evident that you have a lot knowledge on this topic. Your points are well made and relatable. Thanks for writing engaging and interesting material. dggrand123.com

 

==================================================

 

What a fantastic post, appreciate it very much, was wondering do you have a RSS feed where I can register and follow your future articles? Bali Vacation

 

============================================

 

I am often to blogging and that i truly appreciate your posts. This article has really peaks my interest. I am about to bookmark your internet site and maintain checking for brand new details. https://www.dggrand123.com

Avatar_small
SEO 说:
2022年4月07日 23:15

After working with Caldwells, I was very overwhelmed with their products. Being in the industry for years, I could say that it is really hard to find one with high quality materials at an affordable price. https://caldwells.com/interior-doors/stock-now is the link to their site to save time and money as well.

Avatar_small
สล็อต เว็บตรง 说:
2022年5月17日 16:43

It’s awesome to pay a visit to this web page and reading the views of all friends regarding this paragraph, while I am also eager of getting experience. <a href="https://www.xoxowebsites.com">먹튀검증</a>

I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I’ve got you bookmarked to look at new stuff you post… <a href="https://okcasino.com/slot/">สล็อต เว็บตรง</a>

Avatar_small
dark web links 说:
2022年7月03日 19:53

When you have done all of this you should still be wondering what to do if your son is on the dark web links.

Avatar_small
dark web links 说:
2022年7月03日 20:30

Therefore, you should change your password on a regular basis and use a new email address that is not the same as the one that you use for work.

Avatar_small
dark web 说:
2022年7月03日 20:31

Therefore, you should change your password on a regular basis and use a new email address that is not the same as the one that you use for work.

Avatar_small
dark web sites 说:
2022年7月03日 20:45

Also, you should make sure that you always keep your antivirus running in order to protect yourself from any viruses or worms that are sent by hackers online.

Avatar_small
deep web 说:
2022年7月03日 20:57

Some people turn to reverse email searches, while others try to crack the password of websites they are unsure of.

Avatar_small
dark web/deep web/d 说:
2022年8月06日 22:59

It's not uncommon to see links that direct you to music, movies, adult sites, or any other form of content that you can imagine.  dark web links

Avatar_small
dark web/deep web/d 说:
2022年8月06日 23:34

Instead of using deceptive tactics, these people are content to allow other webmasters to use their resources without charging them a fee. Of course, there is always the risk of getting found out (which is why you should make sure you use legitimate links). deep web

Avatar_small
dark web/deep web/d 说:
2022年8月06日 23:52

While this usually takes less time than writing an email to the site or having a chat with someone at the site, it is a step you should not neglect taking.  dark web links

Avatar_small
dark web/deep web/d 说:
2022年8月07日 00:11

Links on the dark Web should be carefully scrutinized because many of them are fake or infected with malware. Some Web sites contain hundreds of link partners that funnel your information to countless other Web sites.  dark web sites

Avatar_small
dark web/deep web/d 说:
2022年8月07日 00:29

Most people have become accustomed to clicking on links that appear to be legitimate and then being redirected to a site that is phony. If you get a warning message saying that your download is fraudulent, then don't proceed and contact the site operator. dark web

Avatar_small
dark web/deep web/d 说:
2022年8月07日 00:58

Affiliate marketing on ClickBank is simple. First, register yourself with them. This can usually be done in a matter of minutes.  work from home jobs

Avatar_small
dark web/deep web/d 说:
2022年8月07日 01:20

If you are still wondering how to start affiliate marketing with ClickBank, this process will help you. Once you have found the products you want to promote, you need to sign up as an affiliate with the affiliate program. affiliate marketing success

Avatar_small
JKBOSE Model Paper C 说:
2022年9月08日 19:06

JKBOSE Model Paper 2023 Class 4 Pdf Download with Answers for English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams at official website. JKBOSE Model Paper Class 4 New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.


登录 *


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