@@ -61,16 +61,14 @@ struct GenerativeAIService {
6161
6262 // Verify the status code is 200
6363 guard response. statusCode == 200 else {
64- Logging . logEvent (
65- level: . error,
66- message: " The server responded with an error: \( response) " ,
67- messageCode: . loadRequestResponseError
64+ VertexLog . error (
65+ code: . loadRequestResponseError,
66+ " The server responded with an error: \( response) "
6867 )
6968 if let responseString = String ( data: data, encoding: . utf8) {
70- Logging . logEvent (
71- level: . error,
72- message: " Response payload: \( responseString) " ,
73- messageCode: . loadRequestResponseErrorPayload
69+ VertexLog . error (
70+ code: . loadRequestResponseErrorPayload,
71+ " Response payload: \( responseString) "
7472 )
7573 }
7674
@@ -117,20 +115,18 @@ struct GenerativeAIService {
117115
118116 // Verify the status code is 200
119117 guard response. statusCode == 200 else {
120- Logging . logEvent (
121- level: . error,
122- message: " The server responded with an error: \( response) " ,
123- messageCode: . loadRequestStreamResponseError
118+ VertexLog . error (
119+ code: . loadRequestStreamResponseError,
120+ " The server responded with an error: \( response) "
124121 )
125122 var responseBody = " "
126123 for try await line in stream. lines {
127124 responseBody += line + " \n "
128125 }
129126
130- Logging . logEvent (
131- level: . error,
132- message: " Response payload: \( responseBody) " ,
133- messageCode: . loadRequestStreamResponseErrorPayload
127+ VertexLog . error (
128+ code: . loadRequestStreamResponseErrorPayload,
129+ " Response payload: \( responseBody) "
134130 )
135131 continuation. finish ( throwing: parseError ( responseBody: responseBody) )
136132
@@ -143,11 +139,7 @@ struct GenerativeAIService {
143139 let decoder = JSONDecoder ( )
144140 decoder. keyDecodingStrategy = . convertFromSnakeCase
145141 for try await line in stream. lines {
146- Logging . logEvent (
147- level: . debug,
148- message: " Stream response: \( line) " ,
149- messageCode: . loadRequestStreamResponseLine
150- )
142+ VertexLog . debug ( code: . loadRequestStreamResponseLine, " Stream response: \( line) " )
151143
152144 if line. hasPrefix ( " data: " ) {
153145 // We can assume 5 characters since it's utf-8 encoded, removing `data:`.
@@ -199,10 +191,9 @@ struct GenerativeAIService {
199191 let tokenResult = await appCheck. getToken ( forcingRefresh: false )
200192 urlRequest. setValue ( tokenResult. token, forHTTPHeaderField: " X-Firebase-AppCheck " )
201193 if let error = tokenResult. error {
202- Logging . logEvent (
203- level: . error,
204- message: " Failed to fetch AppCheck token. Error: \( error) " ,
205- messageCode: . appCheckTokenFetchFailed
194+ VertexLog . error (
195+ code: . appCheckTokenFetchFailed,
196+ " Failed to fetch AppCheck token. Error: \( error) "
206197 )
207198 }
208199 }
@@ -225,10 +216,9 @@ struct GenerativeAIService {
225216 private func httpResponse( urlResponse: URLResponse) throws -> HTTPURLResponse {
226217 // Verify the status code is 200
227218 guard let response = urlResponse as? HTTPURLResponse else {
228- Logging . logEvent (
229- level: . error,
230- message: " Response wasn't an HTTP response, internal error \( urlResponse) " ,
231- messageCode: . generativeAIServiceNonHTTPResponse
219+ VertexLog . error (
220+ code: . generativeAIServiceNonHTTPResponse,
221+ " Response wasn't an HTTP response, internal error \( urlResponse) "
232222 )
233223 throw NSError (
234224 domain: " com.google.generative-ai " ,
@@ -277,14 +267,14 @@ struct GenerativeAIService {
277267 // These errors do not produce specific GenerateContentError or CountTokensError cases.
278268 private func logRPCError( _ error: RPCError) {
279269 if error. isFirebaseMLServiceDisabledError ( ) {
280- Logging . logEvent ( level : . error , message : """
270+ VertexLog . error ( code : . firebaseMLAPIDisabled , """
281271 The Vertex AI for Firebase SDK requires the Firebase ML API `firebaseml.googleapis.com` to \
282272 be enabled for your project. Get started in the Firebase Console \
283273 (https://console.firebase.google.com/project/ \( projectID) /genai/vertex) or verify that the \
284274 API is enabled in the Google Cloud Console \
285275 (https://console.developers.google.com/apis/api/firebaseml.googleapis.com/overview?project= \
286276 \( projectID) ).
287- """ , messageCode : . firebaseMLAPIDisabled )
277+ """ )
288278 }
289279 }
290280
@@ -293,16 +283,11 @@ struct GenerativeAIService {
293283 return try JSONDecoder ( ) . decode ( type, from: data)
294284 } catch {
295285 if let json = String ( data: data, encoding: . utf8) {
296- Logging . logEvent (
297- level: . error,
298- message: " JSON response: \( json) " ,
299- messageCode: . loadRequestParseResponseFailedJSON
300- )
286+ VertexLog . error ( code: . loadRequestParseResponseFailedJSON, " JSON response: \( json) " )
301287 }
302- Logging . logEvent (
303- level: . error,
304- message: " Error decoding server JSON: \( error) " ,
305- messageCode: . loadRequestParseResponseFailedJSONError
288+ VertexLog . error (
289+ code: . loadRequestParseResponseFailedJSONError,
290+ " Error decoding server JSON: \( error) "
306291 )
307292 throw error
308293 }
@@ -329,12 +314,12 @@ struct GenerativeAIService {
329314 }
330315
331316 private func printCURLCommand( from request: URLRequest ) {
332- guard Logging . additionalLoggingEnabled ( ) else {
317+ guard VertexLog . additionalLoggingEnabled ( ) else {
333318 return
334319 }
335320 let command = cURLCommand ( from: request)
336- os_log ( . debug, log: Logging . logObject, """
337- \( Logging . service) Creating request with the equivalent cURL command:
321+ os_log ( . debug, log: VertexLog . logObject, """
322+ \( VertexLog . service) Creating request with the equivalent cURL command:
338323 ----- cURL command -----
339324 \( command, privacy: . private)
340325 ------------------------
0 commit comments