Pass REQUEST from MAYBE-READ-POST-PARAMETERS to RAW-POST-DATA, patch
[hunchentoot.git] / log.lisp
blob8da37376f47d7447c8686c6c18cc018ccbbbc384
1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
2 ;;; $Header: /usr/local/cvsrep/hunchentoot/log.lisp,v 1.10 2008/02/13 16:02:17 edi Exp $
4 ;;; Copyright (c) 2004-2009, Dr. Edmund Weitz. All rights reserved.
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :hunchentoot)
32 (defmacro with-log-file ((stream-var pathname lock) &body body)
33 "Helper macro which executes BODY only if PATHNAME \(which is
34 evaluated) is not NIL. In this case, the file designated by PATHNAME
35 is opened for writing \(appending) and created if it doesn't exist.
36 STREAM-VAR is then bound to a flexi stream which can be used to write
37 characters to the file in UTF-8 format."
38 (with-unique-names (binary-stream)
39 (with-rebinding (pathname)
40 `(when ,pathname
41 (with-lock-held (,lock)
42 (with-open-file (,binary-stream ,pathname
43 :direction :output
44 :element-type 'octet
45 :if-does-not-exist :create
46 :if-exists :append
47 #+:openmcl #+:openmcl
48 :sharing :lock)
49 (let ((,stream-var (make-flexi-stream ,binary-stream :external-format +utf-8+)))
50 ,@body)))))))
52 (defun log-message-to-file (log-level format-string &rest format-arguments)
53 "Sends a formatted message to the file denoted by
54 *MESSAGE-LOG-PATHNAME* \(unless this value is NIL). FORMAT and ARGS
55 are as in FORMAT. LOG-LEVEL is a keyword denoting the log level or
56 NIL in which case it is ignored."
57 (with-log-file (stream *message-log-pathname* *message-log-lock*)
58 (format stream "[~A~@[ [~A]~]] ~?~%"
59 (iso-time) log-level
60 format-string format-arguments)))
62 (defun log-access-to-file (&key return-code content content-length)
63 "Sends a standardized access log message to the file denoted by
64 *ACCESS-LOG-FILE* with information about the current request and
65 response."
66 (with-log-file (stream *access-log-pathname* *access-log-lock*)
67 (format stream "~:[-~@[ (~A)~]~;~:*~A~@[ (~A)~]~] ~:[-~;~:*~A~] [~A] \"~A ~A~@[?~A~] ~
68 ~A\" ~A ~:[~*-~;~D~] \"~:[-~;~:*~A~]\" \"~:[-~;~:*~A~]\"~%"
69 (remote-addr*)
70 (header-in* :x-forwarded-for)
71 (authorization)
72 (iso-time)
73 (request-method*)
74 (script-name*)
75 (query-string*)
76 (server-protocol*)
77 return-code
78 content
79 content-length
80 (referer)
81 (user-agent))))
83 (defun log-message (log-level format-string &rest format-arguments)
84 "Convenience function which calls the message logger of the current
85 acceptor \(if there is one) with the same arguments it accepts.
87 Returns NIL if there is no message logger or whatever the message
88 logger returns.
90 This is the function which Hunchentoot itself uses to log errors it
91 catches during request processing."
92 (when-let (message-logger (acceptor-message-logger *acceptor*))
93 (apply message-logger log-level format-string format-arguments)))