Have you looked at using the fn:trace() or xdmp:trace() functions and filtering 
the log on the event name?

If you called fn:trace() like this:

fn:trace( "This is my application's message", "MyApplication")

It writes an entry to ErrorLog.txt that looks like this;

  2010-03-10 08:26:49.605 Info: [Event:id=MyApplication] This is my 
application's message

It's then easy to read the ErrorLog.txt and return the lines with your 
particular event name ("id=...").

Here is a function that can be used to read any of the logs and return the last 
"$maxentries" that contain the event name:

declare function readLog($filename as xs:string, $maxentries as xs:integer, 
$event as xs:string)
as xs:string*
{
            try {
                        (: We need to restrict reading of logs to the Log 
directory or this becomes an open door
                           to read any file on the host file system. :)
                        if (fn:contains($filename, "/") or 
fn:contains($filename, "\"))
                        then fn:concat("Error: Invalid Log name specified: '", 
$filename, "'")
                        else (
                                    let $pathsep     := if 
(xdmp:platform()="winnt") then "\" else "/"
                                    let $logdir        := 
fn:concat(xdmp:data-directory(), $pathsep, "Logs", $pathsep)
                                    let $logfile       := fn:concat( $logdir, 
$filename)
                                    let $content     := 
xdmp:filesystem-file($logfile)
                                    (: split the output into individual entries 
:)
                                    let $entries       :=
                                                                        if 
($event eq "")
                                                                        then 
fn:tokenize($content,"\n")
                                                                        else 
fn:tokenize($content,"\n")[fn:contains(., $event)]
                                    let $i                := fn:count($entries)
                                    let $first           := fn:max( (1, $i - 
$maxentries) )
                                    return $entries[$first to fn:last()]
                        )
            }
            catch ($e) {
                        fn:concat("Exception thrown reading log: ", 
$e/error:format-string/text() )
            }
};

Note that if you pass an empty event name it will return the last entries of 
the log file.

If you wrap your message in an element name like this:

declare function logInfo($evtid as xs:string, $values as item()*)
as item()*
{
let $msg := element message { $values }
            return fn:trace( $msg, $evtid )
};

Use xdmp:unquote() on the <message> after the event string and you have all the 
power of XQuery for further processing.

Let me know if that helps.

Keith L. Breinholt
breinhol...@ldschurch.org<mailto:breinhol...@ldschurch.org>

From: general-boun...@developer.marklogic.com 
[mailto:general-boun...@developer.marklogic.com] On Behalf Of isa ran
Sent: Wednesday, March 10, 2010 7:57 AM
To: general@developer.marklogic.com
Subject: [MarkLogic Dev General] working with .txt file in MarkLogic

Hi,

Can we load and update a .txt file using MarkLogic/generic xquery functions in 
Marklogic?
Something similar to a ErrorLog.txt and using xdmp:log()......
to be specific a customized log file for the application rather than using the 
ErrorLog.txt.
I don't mind if the .txt file is in the filesystem or the Marklogic database.

Thanking you,
isa.




 NOTICE: This email message is for the sole use of the intended recipient(s) 
and may contain confidential and privileged information. Any unauthorized 
review, use, disclosure or distribution is prohibited. If you are not the 
intended recipient, please contact the sender by reply email and destroy all 
copies of the original message.


_______________________________________________
General mailing list
General@developer.marklogic.com
http://xqzone.com/mailman/listinfo/general

Reply via email to