Skip to content
This repository was archived by the owner on Oct 12, 2018. It is now read-only.

Commit 96dc660

Browse files
committed
Added logging and try/catch for ML and Zorba, along with options to output (save or view) the resulting log.
1 parent 5ce2d7c commit 96dc660

File tree

1 file changed

+106
-8
lines changed

1 file changed

+106
-8
lines changed

xbin/zorba3-0.xqy

+106-8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ declare namespace notes = "http://id.loc.gov/vocabulary/notes/";
5656

5757
declare namespace an = "http://zorba.io/annotations";
5858
declare namespace httpexpath = "http://expath.org/ns/http-client";
59+
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
60+
61+
declare namespace log = "info:lc/marc2bibframe/logging#";
62+
declare namespace err = "http://www.w3.org/2005/xqt-errors";
63+
declare namespace zerror = "http://zorba.io/errors";
5964

6065
(:~
6166
: This variable is for the base uri for your Authorites/Concepts.
@@ -70,7 +75,7 @@ declare variable $baseuri as xs:string external;
7075
declare variable $marcxmluri as xs:string external;
7176

7277
(:~
73-
: This variable is for desired serialzation. Expected values are: rdfxml (default), rdfxml-raw, ntriples, json, exhibitJSON
78+
: This variable is for desired serialzation. Expected values are: rdfxml (default), rdfxml-raw, ntriples, json, exhibitJSON, log
7479
:)
7580
declare variable $serialization as xs:string external;
7681

@@ -79,6 +84,16 @@ declare variable $serialization as xs:string external;
7984
:)
8085
declare variable $resolveLabelsWithID as xs:string external := "false";
8186

87+
(:~
88+
: If set to "true" will write log file to directory.
89+
:)
90+
declare variable $writelog as xs:string external := "false";
91+
92+
(:~
93+
: Directory for log files. MUST end with a slash.
94+
:)
95+
declare variable $logdir as xs:string external := "";
96+
8297
(:~
8398
Performs an http get but does not follow redirects
8499
@@ -188,6 +203,10 @@ declare %an:sequential function local:resolve-labels(
188203
return <rdf:RDF>{$resources}</rdf:RDF>
189204
};
190205

206+
let $startDT := fn:current-dateTime()
207+
let $logfilename := fn:replace(fn:substring-before(xs:string($startDT), "."), "-|:", "")
208+
let $logfilename := fn:concat($logdir, $logfilename, '.log.xml')
209+
191210
let $marcxml :=
192211
if ( fn:starts-with($marcxmluri, "http://" ) or fn:starts-with($marcxmluri, "https://" ) ) then
193212
let $json := http:get($marcxmluri)
@@ -201,16 +220,54 @@ let $marcxml :=
201220
return $mxml
202221
let $marcxml := $marcxml//marcxml:record
203222

204-
let $resources :=
223+
let $result :=
205224
for $r in $marcxml
206225
let $controlnum := xs:string($r/marcxml:controlfield[@tag eq "001"][1])
207226
let $httpuri := fn:concat($baseuri , $controlnum)
208-
let $bibframe := marcbib2bibframe:marcbib2bibframe($r,$httpuri)
209-
return $bibframe/child::node()[fn:name()]
227+
let $r :=
228+
try {
229+
let $rdf := marcbib2bibframe:marcbib2bibframe($r,$httpuri)
230+
let $o := $rdf/child::node()[fn:name()]
231+
let $logmsg :=
232+
element log:success {
233+
attribute uri {$httpuri},
234+
attribute datetime { fn:current-dateTime() }
235+
}
236+
return
237+
element result {
238+
element logmsg {$logmsg},
239+
element rdf {$o}
240+
}
241+
} catch * {
242+
(: Could get entire stack trace from Zorba, but omitting for now. :)
243+
let $stack1 := $zerror:stack-trace
244+
let $logmsg :=
245+
element log:error {
246+
attribute uri {$httpuri},
247+
attribute datetime { fn:current-dateTime() },
248+
element log:error-details {
249+
element log:error-xcode { xs:string($err:code) },
250+
element log:error-description { xs:string($err:description) },
251+
element log:error-file { xs:string($err:module) },
252+
element log:error-line { xs:string($err:line-number) },
253+
element log:error-column { xs:string($err:column-number) }
254+
(: element log:error-stack { $stack1 } :)
255+
},
256+
element log:offending-record {
257+
$r
258+
}
259+
}
260+
return
261+
element result {
262+
element logmsg {$logmsg}
263+
}
264+
}
265+
return
266+
$r
210267

211268
let $rdfxml-raw :=
212269
element rdf:RDF {
213-
$resources
270+
$result//rdf/child::node()[fn:name()]
214271
}
215272

216273
let $rdfxml :=
@@ -224,15 +281,56 @@ let $rdfxml :=
224281
else
225282
$rdfxml-raw
226283

284+
let $endDT := fn:current-dateTime()
285+
let $log :=
286+
element log:log {
287+
attribute engine {"MarkLogic"},
288+
attribute start {$startDT},
289+
attribute end {$endDT},
290+
attribute source {$marcxmluri},
291+
attribute total-submitted { fn:count($marcxml) },
292+
attribute total-success { fn:count($marcxml) - fn:count($result//logmsg/log:error) },
293+
attribute total-error { fn:count($result//logmsg/log:error) },
294+
$result//logmsg/log:*
295+
}
296+
297+
let $logwritten :=
298+
if ($writelog eq "true") then
299+
file:write-text($logfilename, serialize($log,
300+
<output:serialization-parameters>
301+
<output:indent value="yes"/>
302+
<output:method value="xml"/>
303+
<output:omit-xml-declaration value="no"/>
304+
</output:serialization-parameters>)
305+
)
306+
else
307+
()
308+
309+
(:
310+
For now, not injecting notice about an error into the JSON outputs.
311+
There are a couple of ways to do it (one is a hack, the other is the right way)
312+
but 1) will it break anything and 2) is there a need?
313+
:)
227314
let $response :=
228315
if ($serialization eq "ntriples") then
229-
rdfxml2nt:rdfxml2ntriples($rdfxml)
316+
if (fn:count($result//logmsg/log:error) > 0) then
317+
fn:concat("# Errors encountered. View 'log' for details.", fn:codepoints-to-string(10), rdfxml2nt:rdfxml2ntriples($rdfxml))
318+
else
319+
rdfxml2nt:rdfxml2ntriples($rdfxml)
230320
else if ($serialization eq "json") then
231321
rdfxml2json:rdfxml2json($rdfxml)
232322
else if ($serialization eq "exhibitJSON") then
233323
bfRDFXML2exhibitJSON:bfRDFXML2exhibitJSON($rdfxml, $baseuri)
324+
else if ($serialization eq "log") then
325+
$log
234326
else
235-
$rdfxml
236-
327+
if (fn:count($result//logmsg/log:error) > 0) then
328+
element rdf:RDF {
329+
comment {"Errors encountered. View 'log' for details."},
330+
$rdfxml/*
331+
}
332+
else
333+
$rdfxml
334+
237335
return $response
238336

0 commit comments

Comments
 (0)