0

Is it possible to write in the couchdb server log (the one defined by default.ini or local.ini in [log]) from a couchapp? (But from somewhere else than a view)

If that's not possible, maybe there's a workaround which would allow to log successful or unsuccessful authentication attemps in the couchdb server log? I'd like to process this server side and would like to avoid logging all httpd activity and grepping for user logging patterns, which doesn't seem to be easy or pretty...

Cheers,

Jun

jun
  • 759
  • 6
  • 16

2 Answers2

1

A year later I find that it was in fact possible to log from views (or lists or any Javascript Design Doc functions) using the log() function: http://docs.couchdb.org/en/1.6.1/query-server/javascript.html#log

log(message)

Log a message to the CouchDB log (at the INFO level).

Arguments:
message – Message to be logged

function(doc){
    log('Procesing doc ' + doc['_id']);
    emit(doc['_id'], null);
}

After the map function has run, the following line can be found in CouchDB logs (e.g. at /var/log/couchdb/couch.log):

[Sat, 03 Nov 2012 17:38:02 GMT] [info] [<0.7543.0>] OS Process #Port<0.3289> Log :: Processing doc 8d300b86622d67953d102165dbe99467

Who would have guessed :)

jun
  • 759
  • 6
  • 16
0

I'm pretty sure you can't write to couch.log from a view, it's a sandboxed system.

Getting a record of connections to the server is possible though. Here's a dump from my couch.log, with an HTTP error in there:

/
[Sat, 13 Sep 2014 08:18:57 GMT] [info] [<0.160.0>] Opening index for db: test idx: _design/ivet sig: "f6b64ef8593e23cac644c13b895b7607"
[Sat, 13 Sep 2014 08:18:57 GMT] [info] [<0.121.0>] 127.0.0.1 - - GET /test/_design/ivet/_view/medicationWHP/foobar?include_docs=true 200
[Sat, 13 Sep 2014 08:18:57 GMT] [info] [<0.121.0>] 127.0.0.1 - - GET /test/_design/ivet/_view/medicationWHP/foobar?include_docs=true 500
[Sat, 13 Sep 2014 08:18:57 GMT] [error] [<0.121.0>] httpd 500 error response:
 {"error":"json_encode","reason":"{bad_term,{key,null}}"}

[Sat, 13 Sep 2014 08:19:05 GMT] [info] [<0.36.0>] Apache CouchDB has started on http://127.0.0.1:5984/

You can see it has the VERB PATH CODE format for each line, so you can filter that for whatever you need. (Unauthorized is 401) You can also access the log through /_log. Details on that are here:

http://docs.couchdb.org/en/latest/api/server/common.html#log

To get all that information, you'll need to have the log level set to info. You can do this at the config screen in futon.

To do it server-side, you'd probably need to use node.js or something like that. Just have it consume the /_log endpoint, and filter each line by the HTTP response code.

ddouglascarr
  • 1,334
  • 3
  • 14
  • 23
  • Hey, thanks for the answer, I was awere of this and did mention in my question: "would like to avoid logging all httpd activity and grepping for user logging patterns, which doesn't seem to be easy or pretty...". But in any case, good to know that it's probably not possible in another way. Do you see a reason why not though? It doesn't seem that it would be a security concern and it would be useful for a number of things (metrics, custom errors,...) – jun Sep 29 '14 at 14:12
  • I think the reason is architectural. CouchDB and the view servers are actually separate programs which communicate over stdio. The view servers don't have their own logs, and there's nothing in the communication protocol between CouchDB and the view server to facilitate the transmission of log messages. It's not just a simple case of calling log() within the view server. – ddouglascarr Oct 03 '14 at 11:06