const{createApp}=Vue const{createVuetify,useGoTo,useDisplay}=Vuetify var data={alert:{show:false,color:'success',text:'',timeout:0,},theme:{dark:false,},nav:{showDrawer:false,showTOC:true,tocPanel:0,tab:'account',post:{discussionId:3401,currentPage:1,targetPage:1,total:19,anchor:0,goToOptions:{container:null,duration:0,easing:'easeInOutCubic',offset:-100,},worker:null,task:[],active:[],apiLock:[],originLike:new Map([]),},related:{block:1,}},search:{width:80,text:null,loading:false,},tags:[{id:27,url:'/t/0e2e58346a045c7567770b2b3120',name:'Go',color:'#FFA726',icon:'mdi-language-go',},{id:28,url:'/t/3d104b0f6a045c7568772c310313',name:'Java',color:'#FFA726',icon:'mdi-language-java',},],posts:[{id:39450,num:0,uid:9267,content:'\u003Cp\u003E在自己写一个 demo 项目的时候,go 中好像没有 J**a 中的 SLF4J 这样统一的日志接口.在碰到引用第三方的包(像 viper)中使用的日志, 这样日志是不是就是分散的.\u003C/p\u003E',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 12:32:00',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39451,num:1,uid:9207,content:'go 的日志一直是个大问题',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 12:35:54',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39452,num:2,uid:4234,content:'go 的 log 和 error ,一言难尽,只能多体会,没有统一解决方案',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 12:39:00',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39453,num:3,uid:6008,content:'一般完善一点的库都有设置 logger 的接口,你可以用自己喜欢的 log 库,实现别人的接口然后 set 进去。\u003Cbr\u003Ego 的标准库其实提供了统一的 logger 接口和实现 slog ,但是历史遗留比较多,很多人不愿意用。\u003Cbr\u003E\u003Cbr\u003Ego 本身语言特性不是很丰富,所以写代码讲究一个随心所欲,快糙猛就是干,宗旨就是能跑就行。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 12:39:32',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39454,num:4,uid:9267,content:'\u003Cbr\u003E因为没有接触过实际项目, 这样的话, 是不是在实际开发时, 第三方包如果没有出问题, 并不会过多的去关注第三方包的 log',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 12:41:13',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39455,num:5,uid:4234,content:'完全不关注,而且一些 lib 压根就没有日志,你要关心的可能就是 web 框架的日志',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 12:42:22',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39456,num:6,uid:9207,content:'\u003Cimg src\u003D\"https://i.imgur.com/L62ZP7V.png\"\u003E正规的包大部分不会输出 log 的,除非是成型的第三方组件的,反正百花齐放就对了',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 12:43:30',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39457,num:7,uid:9267,content:'很感谢回答, 自己再自身多感受 go 中 log 使用',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 12:45:13',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39458,num:8,uid:90,content:'你现在的话就用 slog 完事\u003Cbr\u003E\u003Cbr\u003E但是之前有很多第三方 log 库,就比较乱',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 13:41:25',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39459,num:9,uid:3005,content:'go 自己的 log 够用,但又似乎没那么好用。—— 也许是我不会用\u003Cbr\u003E\u003Cbr\u003E反正还是用 viper 吧',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 13:43:04',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39460,num:10,uid:9209,content:'标准库已经有了 slog ,相对比较通用的第三方也有 zap ,有日志需求的库一般也有 logger 接口也可以根据需求传入自己的 logger 。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 13:50:11',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39461,num:11,uid:3005,content:'\u003Cbr\u003E\u003Cbr\u003E感谢!原来有 slog 了。。我实在太更不上时代了。。只用上了 ServeMUX 。 之前完全不知道 slog 原来都出来一年多了 !',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 13:53:25',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39462,num:12,uid:9268,content:'用 zap 吧',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 13:55:50',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39463,num:13,uid:8381,content:'确实是大坑,每个库的 logger 接口都不太一致,需要分别兼容。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 13:56:12',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39464,num:14,uid:452,content:'大部分库都不产生 log ,因为有啥问题 error 就丢给你了,让你自己打\u003Cbr\u003E部分 db 或者 web 框架需要你适配个 logger interface 丢进去,比较麻烦,不过好在就只用写一次,以后直接搬就好了',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 15:13:41',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39465,num:15,uid:9267,content:'\u003Cbr\u003E有搜索到 slog 和 zap, 后续会多去了解. \u003Cbr\u003E\u003Cbr\u003E\u003Cbr\u003E认真想 J**a 其实也是用这种方法解决, 很多都是有另外的包转成 SLF4J , 然后再统一使用日志实现.\u003Cbr\u003EJ**a 常用的内容生态已经成熟了, 用习惯了没有能够反应过来.\u003Cbr\u003E\u003Cbr\u003E简单询问 AI 好像 zap 是想要找的东西. 后面学习差不多了, 仔细了解一下.',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 15:35:28',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39466,num:16,uid:5793,content:'go 官方最开始提供的日志接口太烂了,根本没人用。\u003Cbr\u003E然后就百花齐放的出了很多第三方日志库。\u003Cbr\u003E\u003Cbr\u003E现在 go 官方出了标准的日志接口 slog ,但是太晚了,普及起来感觉会很慢。\u003Cbr\u003E\u003Cbr\u003E我跟人建议,新项目就按照 slog 接口,底层自己用 zap 之类的库。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 15:47:30',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39467,num:17,uid:5892,content:'golang logger 是我学 golang 的时候遇到最恶心的东西,尤其各种第三方 client ,一人一个 logger 接口',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 18:04:35',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:39468,num:18,uid:9269,content:'1. go 的几个主要日志库都可以互相套娃做 sink\u003Cbr\u003E2. go 库不太喜欢乱写日志,常见的是乱扔 error 。不过你可以用 errorx 把别人的 error 包一层,保留上下文信息方便调试。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-14 23:08:56',updatedAt:'2025-03-17 11:58:23',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},],usersMap:new Map([[9269,{uid:9269,url:'/u/6a707f356a045c7e626c5f6f0d300405',avatar:'/a/6a707f356a045c7e626c5f6f0d300405',username:'ipwx🤖',}],[4234,{uid:4234,url:'/u/681c5f356a045c736269526f20193221',avatar:'/a/681c5f356a045c736269526f20193221',username:'Ayanokouji🤖',}],[9207,{uid:9207,url:'/u/322174296a045c7e626a516f22303c70',avatar:'/a/322174296a045c7e626a516f22303c70',username:'totoro52🤖',}],[9268,{uid:9268,url:'/u/3039591d6a045c7e626c5e6f03301572',avatar:'/a/3039591d6a045c7e626c5e6f03301572',username:'lizhisty🤖',}],[90,{uid:90,url:'/u/1838562f6a045c776063566f243f3d11',avatar:'/a/1838562f6a045c776063566f243f3d11',username:'tairan2006🤖',}],[3005,{uid:3005,url:'/u/0d3168236a045c74606a536f04580200',avatar:'/a/0d3168236a045c74606a536f04580200',username:'lekai63🤖',}],[9209,{uid:9209,url:'/u/037c07046a045c7e626a5f6f10501109',avatar:'/a/037c07046a045c7e626a5f6f10501109',username:'sthwrong🤖',}],[5892,{uid:5892,url:'/u/1d3347246a045c726863546f200d0720',avatar:'/a/1d3347246a045c726863546f200d0720',username:'xxxccc🤖',}],[5793,{uid:5793,url:'/u/0a2542196a045c726763556f2411120e',avatar:'/a/0a2542196a045c726763556f2411120e',username:'xjzshttps🤖',}],[452,{uid:452,url:'/u/1305021c6a045c77646f546f3a186a73',avatar:'/a/1305021c6a045c77646f546f3a186a73',username:'pkoukk🤖',}],[6008,{uid:6008,url:'/u/1e275a276a045c71606a5e6f01051406',avatar:'/a/1e275a276a045c71606a5e6f01051406',username:'j**alaw2010🤖',}],[8381,{uid:8381,url:'/u/18115e236a045c7f6362576f122b1a1d',avatar:'/a/18115e236a045c7f6362576f122b1a1d',username:'CapNemo🤖',}],[9267,{uid:9267,url:'/u/683a79156a045c7e626c516f4e5c1202',avatar:'/a/683a79156a045c7e626c516f4e5c1202',username:'BenHunDun🤖',}],]),related:[{title:'有在 vscode 上写 Java 的么?一个非常简单的扩展:可以一键复制 arthas 的 watch 命令。',url:'/d/0b26510b6a045c77606a567a455e606a5f011624',},{title:'大佬们平时都是怎样调试查看变量变化的? go 有哪些方便实时展示变量的库或工具?',url:'/d/221f67216a045c77606a567a455c636a23050963',},{title:'「郑州」招聘高级 Java 15K 上下,看个人水平',url:'/d/6a2d46346a045c77606a56754e58616a0e216467',},{title:'Cursor Go 开发,有哪些必装的插件?',url:'/d/337a733f6a045c77606a56754f5b6b6a39061e13',},{title:'在 Go 项目里,涉及到金额计算,大家一般用什么方式?',url:'/d/30267d146a045c77606a5675425d606a3f732227',},{title:'[全职]广州 - 潮汐 App - Golang 开发工程师',url:'/d/022a75356a045c77606a5675445d666a3b301504',},{title:'goland 如何远程开发?',url:'/d/1e1845056a045c77606a56754550676a29726b65',},{title:'开发了一个开箱即用的本地终端 Ztty',url:'/d/0c045a2e6a045c77606a5675455e6b6a3e200734',},{title:'go 部署到 1panel 上,是用源码运行 还是编译二进制运行!',url:'/d/193a435c6a045c77606a5675455c626a38360005',},{title:'[go 分布式定时任务框架] xxljob',url:'/d/357904216a045c77606a5675475f636a041a1663',},{title:'技术栈选择: Java 还是 Python',url:'/d/2878683b6a045c77606a5675475d616a083a1603',},{title:'Java 开发,接下来的路咋走啊?',url:'/d/1f78462e6a045c77606a5675475b656a1c37230a',},{title:'关于 Go 并发获取数据的问题,请教大家',url:'/d/687d67226a045c77606a5674415b646a1d3a3138',},{title:'windsurf 和 cursor 用来写 kotlin 后端还是不太行',url:'/d/2b780b5e6a045c77606a5674425a656a2d103a64',},{title:'Java 后端开发 , macbookAir m4 16+256G 入门款值得入手不,pdd6199 元,(之前那个 MacBookpro 2018 32G+512G 电脑坏了修起来要 4k 以上,没什么维修价值了)另外问一下够用不',url:'/d/200d00356a045c77606a5674435b6b6a0d276310',},{title:'macOS Goland 经常在双击 shift 全局搜索的时候,出现彩虹球,只能强制退出',url:'/d/380f58206a045c77606a5674435b666a15111c2a',},{title:'C++ 开发不想 996,如何跳槽转到 Java ?',url:'/d/09207c086a045c77606a56744358646a1f292613',},{title:'今天有个面试官和我讲 go 的协程比系统的线程更慢,这个我不能理解',url:'/d/182459046a045c77606a56744359626a1412041a',},{title:'Java 和 go 都会,以后找工作偏向哪个方面好呢',url:'/d/000b7b1b6a045c77606a5674445e656a1977262a',},{title:'作为一个传统开发者,如何使用 ai 或者机器学习实现一些简单事情',url:'/d/34015f186a045c77606a5674445e626a25016611',},],} const App={setup(){const goTo=useGoTo() const{mdAndUp}=useDisplay() return{goTo,mdAndUp}},data(){return data;},mounted(){const themeDark=localStorage.getItem("themeDark") if(themeDark!==null){this.theme.dark=JSON.parse(themeDark)} if(this.nav.post.total>(this.nav.post.currentPage-1)*100+20){let moreLen=100 if(this.nav.post.total({id:null,num:(this.nav.post.currentPage-1)*100+v,uid:null,content:null,ipRegion:null,updatedByUid:null,createdAt:null,updatedAt:null,mentionNum:null,mentionedBy:null,mentionUsers:null,likeUsers:null,})) this.posts.push(...morePosts.slice(20))} this.workerStart() const hash=window.location.hash const match=hash.match(/#(\d+)/) if(match){const n=parseInt(match[1],10) if(n>=(this.nav.post.currentPage-1)*100&&n{this.jumpTo(n)})}} this.$nextTick(()=>{this.addHeadingIds() tocbot.init({tocSelector:'.toc',contentSelector:'#post-content-0',headingSelector:'h2, h3, h4',headingsOffset:100,scrollSmoothOffset:-100,scrollSmooth:true,collapseDepth:6,onClick:function(e){setTimeout(()=>{history.replaceState(null,'',window.location.pathname+window.location.search)},0)},}) tocbot.refresh()});},beforeUnmount(){this.workerStop() if(this.quill){this.quill.destroy() this.quill=null}},computed:{dposts(){return this.posts.slice(20);},},created(){},methods:{successAlert(msg){this.alert={show:true,color:'success',text:msg,timeout:1500,}},failureAlert(msg){this.alert={show:true,color:'error',text:msg,timeout:5000,}},flipThemeDark(){this.theme.dark=!this.theme.dark localStorage.setItem("themeDark",JSON.stringify(this.theme.dark))},toSearch(){if(!this.search.text){this.failureAlert('搜索词不能为空') return} let keywords=this.search.text.trim() if(keywords.length<1){this.failureAlert('搜索词不能为空') return} if(keywords.length>100){this.failureAlert('搜索词过长') return} this.doSearch(keywords)},toReg(){window.location.href="/reg"},toLogin(){window.location.href="/login"},toPage(){let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,this.nav.post.targetPage>1?`/${this.nav.post.targetPage}`:'') window.location.href=url},toLoadRelated({done}){if(this.my&&this.my.uid){this.apiLoadRelated({done})}else{done('ok')}},workerStart(){this.nav.post.worker=setInterval(()=>{this.workerLoad()},500);},workerStop(){if(this.nav.post.worker){clearInterval(this.nav.post.worker);this.nav.post.worker=null;}},async jumpTo(num){const page=Math.floor(num/100)+1 const i=num-(page-1)*100 if(page===this.nav.post.currentPage){this.goTo("#post-"+num,this.nav.post.goToOptions) if(!this.posts[i].id){const block=Math.floor(num/20)+1 this.nav.post.apiLock[block]=true await this.apiLoadPosts(block) this.$nextTick(()=>{this.goTo("#post-"+num,this.nav.post.goToOptions)})}}else{let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,page>1?`/${page}`:'') url=url+"#"+num window.location.href=url}},postIntersect(num){return(isIntersecting,entries,observer)=>{if(isIntersecting){this.nav.post.task.push(num) this.nav.post.active.push(num) this.nav.post.active=this.nav.post.active.filter(item=>Math.abs(item-num)<=5) this.nav.post.active.sort((a,b)=>a-b)}else{this.nav.post.active=this.nav.post.active.filter(item=>item!==num)} if(this.nav.post.active[0]){this.nav.post.anchor=this.nav.post.active[0]}else{this.nav.post.anchor=0}}},async apiLoadPosts(block){try{const response=await axios.post('/fapi/v1/post/block/'+block,{discussionId:this.nav.post.discussionId,}) if(response.data.code===0){response.data.data.posts.forEach(post=>{const i=post.num%100 Object.assign(this.posts[i],post)}) response.data.data.users.forEach(user=>{this.usersMap.set(user.uid,user)})}else{this.failureAlert('回帖数据加载失败: '+response.data.msg)}}catch(error){this.failureAlert('回帖数据加载失败: '+error)} this.nav.post.apiLock[block]=false},workerLoad(){while(this.nav.post.task.length){const num=this.nav.post.task.pop() const i=num-(this.nav.post.currentPage-1)*100 if(!this.posts[i].id){const block=Math.floor(num/20)+1 if(!this.nav.post.apiLock[block]){this.nav.post.apiLock[block]=true this.apiLoadPosts(block)}}}},getTimeInfo(t){if(!t){return ""} const now=new Date();const then=new Date(t);const diff=now-then;const minute=60*1000;const hour=minute*60;const day=hour*24;const month=day*30;const year=month*12;if(diffpost.num===num) if(!post){return "#"+num} const uid=post.uid const username=this.usersMap.get(uid)?.username if(!username){return "#"+num} return username},getUsernameByPostId(id){const post=this.posts.find(post=>post.id===id) if(!post){return "#"+this.getPostNumByPostId(id)} const uid=post.uid const username=this.usersMap.get(uid).username if(!username){return "#"+this.getPostNumByPostId(id)} return username},getPostNumByPostId(id){const post=this.posts.find(post=>post.id===id) return post.num},getPostById(id){const post=this.posts.find(post=>post.id===id) return post},getPostByNum(num){const post=this.posts.find(post=>post.num===num) return post},getAvatarByUid(uid){const avatar=this.usersMap.get(uid)?.avatar if(!avatar){return this.getRandomAvatar()} return avatar},getAvatarByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post){return this.getRandomAvatar()} const uid=post.uid return this.getAvatarByUid(uid)},getRandomAvatar(){const num=Math.floor(Math.random()*100) return "https://randomuser.me/api/portraits/men/"+num+".jpg"},getUrlByUid(uid){const url=this.usersMap.get(uid)?.url if(!url){return ""} return url},getTextByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post||!post.content){return '点击跳转到#'+num+'查看'} const parser=new DOMParser() const doc=parser.parseFromString(post.content,'text/html') const text=doc.body.textContent||'' return text.slice(0,100)},addHeadingIds(){const content=document.getElementById('post-content-0') if(!content){this.nav.showTOC=false return} const headings=content.querySelectorAll('h2, h3, h4') headings.forEach((heading,index)=>{if(!heading.id){heading.id=`toc-nav-${index}`}}) if(headings.length==0){this.nav.showTOC=false}},async doSearch(keywords){this.search.loading=true try{const response=await axios.post('/fapi/v1/search',{keywords:keywords,}) if(response.data.code===0){if(response.data.data.hash&&response.data.data.hash.length===32){window.location.href="/s/"+response.data.data.hash}else{this.failureAlert('搜索失败: 搜索服务异常')}}else{this.failureAlert('搜索失败: '+response.data.msg)}}catch(error){this.failureAlert('搜索失败: '+error)} this.search.loading=false},debounce(fn,delay){let timer=null return function(...args){if(timer)clearTimeout(timer) timer=setTimeout(()=>{fn.apply(this,args)},delay);};},},watch:{'nav.post.targetPage':{handler:async function(newV,oldV){this.toPage()},immediate:false},},} const vuetify=createVuetify({defaults:{global:{ripple:true,},},}) const app=createApp(App) app.use(vuetify).mount("#app")