Custome Logger using LoggerWithConfig in Echo

WebFramework EchoのLoggerを弄っていて気付いた点があったのでメモ。 sample code 箇条書き responseログにHeader X-Any-Headerを出力したい場合、ログフォーマットに"any_header":${header:x-any-header}を指定すればいいよ responseログにQueryParamerter ?anyparam=xxxを出力したい場合、ログフォーマットに"any_query":${query:anyparam}を指定すればいいよ デフォルトで出力されるカラム’id’はHeader X-Request-Id を指定すると出力されるよ Skipperを設定することで、path毎にログ出力する/しない等を設定出来るよ 1, 2, 3はEchoのそういう機能だってことで特にありません。 4は結構便利だなと思いました。以下のサンプルコードでは、/healthcheckに来た場合とCI/CD等での自動テスト時にはresponseログを出力しないようにする設定例です。 sample 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 // Middleware e.Use(middleware.LoggerWithConfig(customLogger())) // Handler e.GET("/greet", greet) e.GET("/healthcheck", healthcheck) var ResponseLogForamt = `{` + `"time":"${time_custom}",` + `"id":"${id}",` + `"remote_ip":"${remote_ip}",` + `"host":"${host}",` + `"method":"${method}",` + `"uri":"${uri}",` + `"user_agent":"${user_agent}",` + `"status":${status},` + `"error":"${error}",` + `"latency":${latency},` + `"latency_human":"${latency_human}",` + `"bytes_in":${bytes_in},` + `"bytes_out":${bytes_out},` + `"forwarded-for":"${header:x-forwarded-for}",` + `"same-as-id":${header:X-Request-Id},` + `"query":${query:lang}` + `}` func customLogger() middleware.LoggerConfig { cl := middleware.DefaultLoggerConfig cl.Skipper = customeSkipper cl.Format = ResponseLogForamt cl.CustomTimeFormat = "2006/01/02 15:04:05.00000" return cl } func customeSkipper(c echo.Context) bool { if c.Path() == "/healthcheck" { return true } if os.Getenv("ENV") == "auto-test" { return true } else { return false } } func greet(c echo.Context) error { switch c.QueryParam("lang") { case "jp": return c.String(http.StatusOK, "こんにちは\n") case "en": return c.String(http.StatusOK, "Hello World\n") default: return c.String(http.StatusOK, "ウホホイ!!\n") } } func healthcheck(c echo.Context) error { return c.String(http.StatusOK, "I'm fine\n") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 -- request $ curl \ -X GET \ -H "X-Forwarded-For: achoo" \ -H "X-Request-Id: requestid123" \ "http://localhost/greet?lang=en" -- response log { "time":"2020/11/10 00:17:09.28534", "id":"requestid123", "remote_ip":"achoo", "host":"localhost", "method":"GET", "uri":"/greet?lang=en", "user_agent":"curl/7.64.1", "status":200, "error":"", "latency":18699, "latency_human":"18.699µs", "bytes_in":0, "bytes_out":12, "forwarded-for":"achoo", "same-as-id":requestid123, "query":en }

2020年11月10日 · 2 分

自分用日本語訳 Go Module

(この記事は、執筆時点でのGo Modulesをもとにした私のメモです。Go Moduleの理解のために、元記事と記載の順番が入れ替わっている項目もあります。) ...

2020年5月14日 · 2 分

New Github Official CLI `gh`

github.com/cli/cli/cmd/gh is new official command for GitHub. ...

2020年2月14日 · 1 分

From denite.nvim To fzf.vim

denite.nvimとfzf.vimって比較記事? 違います。断捨離した結果、fzf.vimで事足りてしまったという記事です。 denite.nvimとfzf.vimは、一見やれることが似ているように見えますが提供しているインタフェースが違います。 denite.nvimの方が拡張性/汎用性が高いです。Pythonスクリプトを呼び出せますし。 どうしてやめたん? Python3とpipの環境整備に疲れたというのが理由で完全に力不足なだけです。 そもそも使いこなせていなかったっていうのも大きい。自分に必要な機能が何か見直したら次のがあれば十分っぽい。 コマンドの結果の一覧表示(x-motemen/ghq list, mattn/memo list, history等) 一覧表示の後のアクションを指定可能(cd, vim) buffer切替 fzf.vimはfzfのついでに入れていただけで全く使っていなかった。Shougo/denite.nvimでfzf.vimで同じことが出来るし、sourceの拡張がいくつもあるのでそれで十分だった。 もともとfzfが好きなのもあって、fzf.vimで上記が実現出来るように設定した、っていうかhelpからパクってきた。 何日か使っていて快適に使えているので結構満足。previewはbatってコマンド入れないとsyntax highlightされなかったのでいれたけど、なんとなくそっちの方がリッチっぽいという理由だけなので重ければそのうち消す。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 command! -bang -nargs=? -complete=dir Files call fzf#vim#files( \ <q-args>, \ fzf#vim#with_preview(), \ <bang>0) command! -nargs=0 Ghq call fzf#run({ \ 'source' : 'ghq list --full-path', \ 'sink' : 'cd' \}) command! -nargs=0 Mru call fzf#run({ \ 'source' : v:oldfiles, \ 'sink' : 'edit', \ 'options' : '-m -x +s', \ 'down' : '40%' \}) command! -bang -nargs=? Memo call fzf#vim#files( \ expand($HOME . '/.config/memo/_posts'), \ fzf#vim#with_preview({'options': ['--layout=reverse', '--info=inline']}), \ <bang>0) command! -bang -nargs=* Rg call fzf#vim#grep( \ 'rg --column --no-heading --color=always --smart-case '.shellescape(<q-args>), \ 1, \ fzf#vim#with_preview(), <bang>0) nmap ,f :Files<CR> nmap ,s :Snippet<CR> nmap ,b :Buffers<CR> nmap ,g :Ghq<CR> nmap ,m :Mru<CR> nmap ,mm :Memo<CR> nmap ,rg :Rg<CR>

2020年2月11日 · 1 分

My Favorite Usage urfave/cli

参加しているプロジェクトでurfave/cliを使っている。使い方はExampleにあるのと同じ書き方で使っている。 大半のバッチ処理をコマンドとして記述しているので、Exampleの書き方だとだいぶ見辛くなってきた。縦長のコマンド定義とオプション説明で目的の処理を探すのにもページ送りを何度もする。なんとかしたい。 ...

2020年2月6日 · 1 分

Error 'undefined' when go run

x…motemen/ghqを写経している時、 go run main.go 出来ないことに気付いた。 下記が実行時のエラーになる。ちなみに、 go build は出来る。 1 2 3 4 ~/d/s/g/x/ghq >>> go run main.go # command-line-arguments ./main.go:38:17: undefined: commands ~/d/s/g/x/ghq >>> commands が見つからない?同じ階層の commands.go には下記記述がちゃんとあるのに、どうして見つからない…。 1 2 3 4 5 6 var commands = []*cli.Command{ commandGet, commandList, commandRoot, commandCreate, } 理由は、go runの引数に指定したファイル(+ importされるパッケージ)しか読み込まないから。 ...

2020年2月4日 · 1 分

aws-cliで始めるCloudFormation

目的 CloudFormationで使われる用語を理解する CloudFormationの構成を理解する 出来るだけaws-cliのコマンドを実行していく ...

2019年3月5日 · 1 分

LSP(Language Server Protocol)のドキュメントを読む

golangコミュニティがgoplsを開発し始めたり、Microsoftが提唱したこともあってLanguage Server Protocol (以後,LSP)が注目され始めた。 特にVimやgolang界隈ではmattnさんのブログ記事gocode やめます(そして Language Server へ)で一層注目を集めたと思う。是非読んで欲しい。 gocode contribuerへの思いとLSP発展への期待が書かれている。 そんな熱いLSPを理解することでVim発展に拍車がかかることへの期待とgoplsを理解したいという興味から、まずはLSPドキュメントを訳してみた。 実際に、vim-lsp+goplsを入れて、log出力と照らし合わせながらLSPがどういう風に動くものなのか、理解が深まったと思う。以下、ドキュメントの訳。 ...

2019年2月23日 · 1 分

Javaのチューニングオプション

Options -Dはシステムのオプション -XX:はJava Hotspot VMのオプション # Optinos_list -XX:オプションに対して、+で有効、-で無効を表す。 ...

2018年12月2日 · 1 分

ansibleでの各roleのパス取得方法

playbookをrole毎に分けた際、roleの中でfilesのpath指定をするために実行中の/path/to/role/{current}を知りたくなった。 ref: Accessiing information about other hosts with magic variables ...

2018年10月15日 · 1 分