'integer', 'start_time' => 'datetime', 'end_time' => 'datetime', 'parameters' => 'array', 'execution_count' => 'integer', 'success_count' => 'integer', 'error_count' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime' ]; /** * ステータスの定数 */ const STATUS_START = 'start'; // 開始 const STATUS_RUNNING = 'running'; // 実行中 const STATUS_SUCCESS = 'success'; // 成功 const STATUS_ERROR = 'error'; // エラー const STATUS_WARNING = 'warning'; // 警告 const STATUS_CANCELLED = 'cancelled'; // キャンセル /** * 通用バッチログ作成メソッド * * 任意のバッチ処理で使用可能な統一ログ記録機能 * 実際の実行コマンド名をそのまま記録 * * @param string $processName プロセス名(shj1, shj9:daily等) * @param string $status ステータス * @param array $parameters パラメーター配列 * @param string $message メッセージ * @param array $additionalData 追加データ(device_id等) * @return BatchLog 作成されたバッチログ */ public static function createBatchLog( string $processName, string $status, array $parameters = [], string $message = '', array $additionalData = [] ): BatchLog { // パラメーターに追加データをマージ $allParameters = array_merge($parameters, $additionalData, [ 'executed_at' => now()->toISOString() ]); return self::create([ 'process_name' => $processName, 'status' => $status, 'start_time' => now(), 'end_time' => ($status === self::STATUS_SUCCESS || $status === self::STATUS_ERROR) ? now() : null, 'parameters' => $allParameters, 'message' => $message, 'error_details' => ($status === self::STATUS_ERROR) ? $message : null, 'execution_count' => 1, 'success_count' => $status === self::STATUS_SUCCESS ? 1 : 0, 'error_count' => $status === self::STATUS_ERROR ? 1 : 0 ]); } /** * 成功時のステータスコメント生成 * * @return string ステータスコメント */ public static function getSuccessComment(): string { return '処理成功'; } /** * エラー時のステータスコメント生成 * * @param string $errorMessage エラーメッセージ * @return string ステータスコメント */ public static function getErrorComment(string $errorMessage): string { return 'エラー: ' . $errorMessage; } /** * バッチログの文字列表現 * * @return string */ public function __toString(): string { return sprintf( 'BatchLog[ID:%d, Process:%s, Status:%s, Time:%s]', $this->id, $this->process_name, $this->status, $this->start_time ? $this->start_time->format('Y-m-d H:i:s') : 'N/A' ); } }