Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Stacktrace for rs/zerolog users
Search
masso
May 10, 2024
Technology
0
180
Stacktrace for rs/zerolog users
Asakusa.go #2 LT from masso
About:
- How to use stacktrace in rs/zerolog
masso
May 10, 2024
Tweet
Share
More Decks by masso
See All by masso
データ解釈学入門 第一部 / Data hermeneutics Part 1
masso
8
2.1k
時系列分析と状態空間モデリングの基礎 / Foundations of Time Series Analysis and State Space Models 0
masso
0
530
わかりやすいパターン認識2章 / Pattern Recognition Manual Easy to understand SS 02
masso
0
810
分析環境紹介LT / the introduction of as my analysis env is
masso
0
100
わかりやすいパターン認識1章 / Pattern Recognition Manual Easy to understand SS 01
masso
0
150
データ解析のための統計モデリング入門6章 / Handbook-of-statistical-modeling-for-data-analysis-section6
masso
0
500
DLGが目指すコミュニティの形 / DLG Community Objective
masso
0
2.5k
PowerAutomateによる社員健康状態集計システム / Employee health status tabulation system with Power Automate
masso
0
1.4k
Other Decks in Technology
See All in Technology
Storage Browser for Amazon S3
miu_crescent
1
140
Postman と API セキュリティ / Postman and API Security
yokawasa
0
200
Oracle Cloud Infrastructure:2024年12月度サービス・アップデート
oracle4engineer
PRO
0
170
社外コミュニティで学び社内に活かす共に学ぶプロジェクトの実践/backlogworld2024
nishiuma
0
260
AI時代のデータセンターネットワーク
lycorptech_jp
PRO
1
280
サイボウズフロントエンドエキスパートチームについて / FrontendExpert Team
cybozuinsideout
PRO
5
38k
統計データで2024年の クラウド・インフラ動向を眺める
ysknsid25
2
840
Amazon SageMaker Unified Studio(Preview)、Lakehouse と Amazon S3 Tables
ishikawa_satoru
0
150
AWS re:Invent 2024で発表された コードを書く開発者向け機能について
maruto
0
190
日本版とグローバル版のモバイルアプリ統合の開発の裏側と今後の展望
miichan
1
130
Qiita埋め込み用スライド
naoki_0531
0
860
宇宙ベンチャーにおける最近の情シス取り組みについて
axelmizu
0
110
Featured
See All Featured
KATA
mclloyd
29
14k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Unsuck your backbone
ammeep
669
57k
The Cost Of JavaScript in 2023
addyosmani
45
7k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
Agile that works and the tools we love
rasmusluckow
328
21k
Code Review Best Practice
trishagee
65
17k
YesSQL, Process and Tooling at Scale
rocio
169
14k
How to train your dragon (web standard)
notwaldorf
88
5.7k
How to Ace a Technical Interview
jacobian
276
23k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
Transcript
stacktrace for zerolog users zerolog 使いのための stacktrace Asakusa.go #2 -
Title 2024/05/10 @masso 1
Index type Index struct { Title string PresentationLength float64 //
[minutes] } var indexes = []Index{ {"Self Introduction", 0.5}, {"We're using zerolog", 0.5}, {"The mechanism of stacktrace in zerolog", 1}, {"How to customize stacktrace in zerolog", 1.5}, {"Q&A", 1}, } Asakusa.go #2 - Title 2024/05/10 @masso 2
Self Introduction GitHub: @sota0121 I'm working at Money Forward, Inc.
as a full- stack software engineer. Go, TypeScript, React, Next.js, AWS, k8s, Terraform, etc. Asakusa.go #2 - Title 2024/05/10 @masso 3
We're using zerolog rs/zerolog: Zero Allocation JSON Logger Structured logging
with simple API that is inspired by go.uber.org/zap Main features are here Asakusa.go #2 - Title 2024/05/10 @masso 4
The mechanism of stacktrace in zerolog - basic usage import
( "github.com/rs/zerolog" "github.com/rs/zerolog/pkgerrors" ) zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack func printStackTrace(err error) { // setLogFields ze := log.WithLevel(zerolog.ErrorLevel).Caller(2) // Stacktrace ze.Stack().Err(err).Msg("") } Asakusa.go #2 - Title 2024/05/10 @masso 5
The mechanism of stacktrace in zerolog details main codes rs/zerolog/event.go,
rs/zerolog/globals.go, rs/zerolog/pkgerrors/stacktrace.go stackTracer interface を満たすものを Unwrap しながら探る // pkgerrors/stacktrace.go import "github.com/pkg/errors" func MarshalStack(err error) interface{} { type stackTracer interface { StackTrace() errors.StackTrace } var sterr stackTracer var ok bool for err != nil { sterr, ok = err.(stackTracer) if ok { break } u, ok := err.(interface { Unwrap() error }) if !ok { return nil } err = u.Unwrap() } if sterr == nil { return nil } Asakusa.go #2 - Title 2024/05/10 @masso 6
How to customize stacktrace in zerolog 自前のスタックトレース処理を利用したい場合は、 zerolog.ErrorStackMarshaler に設定する signature:
func(err error) interface{} import "github.com/pkg/errors" func CustomFormatStackTrace(err error) string { if err, ok := err.(interface{ StackTrace() errors.StackTrace }); ok { stack := err.StackTrace() formatted := "" for _, frame := range stack { formatted += fmt.Sprintf("%+v\n", frame) } return formatted } return "No stack trace available" } Asakusa.go #2 - Title 2024/05/10 @masso 7
Q&A Asakusa.go #2 - Title 2024/05/10 @masso 8