以下、関数アプリはカスタムハンドラーを利用した Goの実装となっている為、デフォルトでサポートされている言語の場合と異なる箇所もある。
関数アプリのログを一定期間保持しておきたい場合は、Application Insight を有効化した状態で Functions を作成しておく。
# application insight コンポーネントの作成 az monitor app-insights component create \ --app $insightsName \ --location $insightsRegion \ --resource-group $resourceGroup \ --query-access Enabled \ --retention-time $insightsDays \ --subscription $subscriptionId # 関数アプリの作成 az functionapp create \ --name $funcAppName \ : --app-insights $insightsName <-- これ
※ retention-time は ログの保持期間(日数)で 30,60,90,120,180,270,365,550,730 の何れかを設定する。
カスタムハンドラを使用した関数アプリの場合は、Insight に扱ってもらう為には、戻り値として Logs を返却しておく必要がある。
※https://docs.microsoft.com/ja-jp/azure/azure-functions/functions-custom-handlers#response-payload
goの場合
logs := make([]string, 0) : : logs = append(logs, fmt.Sprintf("[INFO] %s", "サンプルログ1")) logs = append(logs, fmt.Sprintf("[INFO] %s", "サンプルログ2")) fmt.Printf("[INFO] 標準出力にメッセージを出力") invokeResponse := InvokeResponse{Logs: logs, ReturnValue: string(returnValue)} js, err := json.Marshal(invokeResponse) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") w.Write(js)
以下に説明があるように、関数アプリのコードは、ファイル共有を利用して各インスタンスにマウントされる。
https://docs.microsoft.com/ja-jp/azure/azure-functions/storage-considerations
で、このPATH配下にアプリケーションのログも格納されており、ストレージエクスプローラで見る場合ログのPATHは以下の通り。
ログ | 種別 | PATH |
関数アプリのログ | File Shares(ファイル共有) | 関数アプリ名/LogFiles/Application/Functions/Function 配下 |
ホストのログ | File Shares(ファイル共有) | 関数アプリ名/LogFiles/Application/Functions/Host 配下 |
Azure Monitor でログの検索、アラートの設定を行う事が出来る。
クエリサンプル
traces | where operation_Name=="BlobTrigger2" and message has_cs "test" traces | summarize errorCount=countif((operation_Name=="BlobTrigger2" and message has_cs "test" and timestamp > ago(1h)) > 0)