changeset 6678:0125b151c9a5

Stream: log module.
author Vladimir Homutov <vl@nginx.com>
date Mon, 05 Sep 2016 17:50:16 +0300
parents c02290241cbe
children 40e8ce405859
files auto/modules src/stream/ngx_stream.h src/stream/ngx_stream_handler.c src/stream/ngx_stream_log_module.c src/stream/ngx_stream_script.c src/stream/ngx_stream_script.h
diffstat 6 files changed, 343 insertions(+), 581 deletions(-) [+]
line wrap: on
line diff
--- a/auto/modules
+++ b/auto/modules
@@ -971,6 +971,7 @@ if [ $STREAM != NO ]; then
 
     ngx_module_name="ngx_stream_module \
                      ngx_stream_core_module \
+                     ngx_stream_log_module \
                      ngx_stream_proxy_module \
                      ngx_stream_upstream_module"
     ngx_module_incs="src/stream"
@@ -984,6 +985,7 @@ if [ $STREAM != NO ]; then
                      src/stream/ngx_stream_script.c \
                      src/stream/ngx_stream_handler.c \
                      src/stream/ngx_stream_core_module.c \
+                     src/stream/ngx_stream_log_module.c \
                      src/stream/ngx_stream_proxy_module.c \
                      src/stream/ngx_stream_upstream.c \
                      src/stream/ngx_stream_upstream_round_robin.c"
--- a/src/stream/ngx_stream.h
+++ b/src/stream/ngx_stream.h
@@ -121,6 +121,7 @@ typedef struct {
 
     ngx_stream_access_pt           limit_conn_handler;
     ngx_stream_access_pt           access_handler;
+    ngx_stream_access_pt           access_log_handler;
 
     ngx_hash_t                     variables_hash;
 
--- a/src/stream/ngx_stream_handler.c
+++ b/src/stream/ngx_stream_handler.c
@@ -311,11 +311,19 @@ ngx_stream_ssl_handshake_handler(ngx_con
 void
 ngx_stream_finalize_session(ngx_stream_session_t *s, ngx_uint_t rc)
 {
+    ngx_stream_core_main_conf_t  *cmcf;
+
     ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
                    "finalize stream session: %i", rc);
 
     s->status = rc;
 
+    cmcf = ngx_stream_get_module_main_conf(s, ngx_stream_core_module);
+
+    if (cmcf->access_log_handler) {
+        (void) cmcf->access_log_handler(s);
+    }
+
     ngx_stream_close_connection(s->connection);
 }
 
copy from src/http/modules/ngx_http_log_module.c
copy to src/stream/ngx_stream_log_module.c
--- a/src/http/modules/ngx_http_log_module.c
+++ b/src/stream/ngx_stream_log_module.c
@@ -7,169 +7,148 @@
 
 #include <ngx_config.h>
 #include <ngx_core.h>
-#include <ngx_http.h>
+#include <ngx_stream.h>
 
 #if (NGX_ZLIB)
 #include <zlib.h>
 #endif
 
 
-typedef struct ngx_http_log_op_s  ngx_http_log_op_t;
+typedef struct ngx_stream_log_op_s  ngx_stream_log_op_t;
 
-typedef u_char *(*ngx_http_log_op_run_pt) (ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
+typedef u_char *(*ngx_stream_log_op_run_pt) (ngx_stream_session_t *s,
+    u_char *buf, ngx_stream_log_op_t *op);
 
-typedef size_t (*ngx_http_log_op_getlen_pt) (ngx_http_request_t *r,
+typedef size_t (*ngx_stream_log_op_getlen_pt) (ngx_stream_session_t *s,
     uintptr_t data);
 
 
-struct ngx_http_log_op_s {
-    size_t                      len;
-    ngx_http_log_op_getlen_pt   getlen;
-    ngx_http_log_op_run_pt      run;
-    uintptr_t                   data;
+struct ngx_stream_log_op_s {
+    size_t                       len;
+    ngx_stream_log_op_getlen_pt  getlen;
+    ngx_stream_log_op_run_pt     run;
+    uintptr_t                    data;
 };
 
 
 typedef struct {
-    ngx_str_t                   name;
-    ngx_array_t                *flushes;
-    ngx_array_t                *ops;        /* array of ngx_http_log_op_t */
-} ngx_http_log_fmt_t;
+    ngx_str_t                    name;
+    ngx_array_t                 *flushes;
+    ngx_array_t                 *ops;        /* array of ngx_stream_log_op_t */
+} ngx_stream_log_fmt_t;
 
 
 typedef struct {
-    ngx_array_t                 formats;    /* array of ngx_http_log_fmt_t */
-    ngx_uint_t                  combined_used; /* unsigned  combined_used:1 */
-} ngx_http_log_main_conf_t;
+    ngx_array_t                  formats;    /* array of ngx_stream_log_fmt_t */
+} ngx_stream_log_main_conf_t;
 
 
 typedef struct {
-    u_char                     *start;
-    u_char                     *pos;
-    u_char                     *last;
+    u_char                      *start;
+    u_char                      *pos;
+    u_char                      *last;
 
-    ngx_event_t                *event;
-    ngx_msec_t                  flush;
-    ngx_int_t                   gzip;
-} ngx_http_log_buf_t;
+    ngx_event_t                 *event;
+    ngx_msec_t                   flush;
+    ngx_int_t                    gzip;
+} ngx_stream_log_buf_t;
 
 
 typedef struct {
-    ngx_array_t                *lengths;
-    ngx_array_t                *values;
-} ngx_http_log_script_t;
+    ngx_array_t                 *lengths;
+    ngx_array_t                 *values;
+} ngx_stream_log_script_t;
 
 
 typedef struct {
-    ngx_open_file_t            *file;
-    ngx_http_log_script_t      *script;
-    time_t                      disk_full_time;
-    time_t                      error_log_time;
-    ngx_syslog_peer_t          *syslog_peer;
-    ngx_http_log_fmt_t         *format;
-    ngx_http_complex_value_t   *filter;
-} ngx_http_log_t;
+    ngx_open_file_t             *file;
+    ngx_stream_log_script_t     *script;
+    time_t                       disk_full_time;
+    time_t                       error_log_time;
+    ngx_syslog_peer_t           *syslog_peer;
+    ngx_stream_log_fmt_t        *format;
+    ngx_stream_complex_value_t  *filter;
+} ngx_stream_log_t;
 
 
 typedef struct {
-    ngx_array_t                *logs;       /* array of ngx_http_log_t */
+    ngx_array_t                 *logs;       /* array of ngx_stream_log_t */
 
-    ngx_open_file_cache_t      *open_file_cache;
-    time_t                      open_file_cache_valid;
-    ngx_uint_t                  open_file_cache_min_uses;
+    ngx_open_file_cache_t       *open_file_cache;
+    time_t                       open_file_cache_valid;
+    ngx_uint_t                   open_file_cache_min_uses;
 
-    ngx_uint_t                  off;        /* unsigned  off:1 */
-} ngx_http_log_loc_conf_t;
+    ngx_uint_t                   off;        /* unsigned  off:1 */
+} ngx_stream_log_srv_conf_t;
 
 
 typedef struct {
-    ngx_str_t                   name;
-    size_t                      len;
-    ngx_http_log_op_run_pt      run;
-} ngx_http_log_var_t;
+    ngx_str_t                    name;
+    size_t                       len;
+    ngx_stream_log_op_run_pt     run;
+} ngx_stream_log_var_t;
 
 
-static void ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log,
+static void ngx_stream_log_write(ngx_stream_session_t *s, ngx_stream_log_t *log,
     u_char *buf, size_t len);
-static ssize_t ngx_http_log_script_write(ngx_http_request_t *r,
-    ngx_http_log_script_t *script, u_char **name, u_char *buf, size_t len);
+static ssize_t ngx_stream_log_script_write(ngx_stream_session_t *s,
+    ngx_stream_log_script_t *script, u_char **name, u_char *buf, size_t len);
 
 #if (NGX_ZLIB)
-static ssize_t ngx_http_log_gzip(ngx_fd_t fd, u_char *buf, size_t len,
+static ssize_t ngx_stream_log_gzip(ngx_fd_t fd, u_char *buf, size_t len,
     ngx_int_t level, ngx_log_t *log);
 
-static void *ngx_http_log_gzip_alloc(void *opaque, u_int items, u_int size);
-static void ngx_http_log_gzip_free(void *opaque, void *address);
+static void *ngx_stream_log_gzip_alloc(void *opaque, u_int items, u_int size);
+static void ngx_stream_log_gzip_free(void *opaque, void *address);
 #endif
 
-static void ngx_http_log_flush(ngx_open_file_t *file, ngx_log_t *log);
-static void ngx_http_log_flush_handler(ngx_event_t *ev);
+static void ngx_stream_log_flush(ngx_open_file_t *file, ngx_log_t *log);
+static void ngx_stream_log_flush_handler(ngx_event_t *ev);
 
-static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_iso8601(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_body_bytes_sent(ngx_http_request_t *r,
-    u_char *buf, ngx_http_log_op_t *op);
-static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-
-static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
-    ngx_http_log_op_t *op, ngx_str_t *value);
-static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
+static ngx_int_t ngx_stream_log_variable_compile(ngx_conf_t *cf,
+    ngx_stream_log_op_t *op, ngx_str_t *value);
+static size_t ngx_stream_log_variable_getlen(ngx_stream_session_t *s,
     uintptr_t data);
-static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src, size_t size);
+static u_char *ngx_stream_log_variable(ngx_stream_session_t *s, u_char *buf,
+    ngx_stream_log_op_t *op);
+static uintptr_t ngx_stream_log_escape(u_char *dst, u_char *src, size_t size);
 
 
-static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
-static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
-static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
+static void *ngx_stream_log_create_main_conf(ngx_conf_t *cf);
+static void *ngx_stream_log_create_srv_conf(ngx_conf_t *cf);
+static char *ngx_stream_log_merge_srv_conf(ngx_conf_t *cf, void *parent,
     void *child);
-static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
+static char *ngx_stream_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
-static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
+static char *ngx_stream_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
-static char *ngx_http_log_compile_format(ngx_conf_t *cf,
+static char *ngx_stream_log_compile_format(ngx_conf_t *cf,
     ngx_array_t *flushes, ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s);
-static char *ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
+static char *ngx_stream_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
-static ngx_int_t ngx_http_log_init(ngx_conf_t *cf);
+static ngx_int_t ngx_stream_log_init(ngx_conf_t *cf);
 
 
-static ngx_command_t  ngx_http_log_commands[] = {
+static ngx_command_t  ngx_stream_log_commands[] = {
 
     { ngx_string("log_format"),
-      NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
-      ngx_http_log_set_format,
-      NGX_HTTP_MAIN_CONF_OFFSET,
+      NGX_STREAM_MAIN_CONF|NGX_CONF_2MORE,
+      ngx_stream_log_set_format,
+      NGX_STREAM_MAIN_CONF_OFFSET,
       0,
       NULL },
 
     { ngx_string("access_log"),
-      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
-                        |NGX_HTTP_LMT_CONF|NGX_CONF_1MORE,
-      ngx_http_log_set_log,
-      NGX_HTTP_LOC_CONF_OFFSET,
+      NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_1MORE,
+      ngx_stream_log_set_log,
+      NGX_STREAM_SRV_CONF_OFFSET,
       0,
       NULL },
 
     { ngx_string("open_log_file_cache"),
-      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
-      ngx_http_log_open_file_cache,
-      NGX_HTTP_LOC_CONF_OFFSET,
+      NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1234,
+      ngx_stream_log_open_file_cache,
+      NGX_STREAM_SRV_CONF_OFFSET,
       0,
       NULL },
 
@@ -177,26 +156,23 @@ static ngx_command_t  ngx_http_log_comma
 };
 
 
-static ngx_http_module_t  ngx_http_log_module_ctx = {
+static ngx_stream_module_t  ngx_stream_log_module_ctx = {
     NULL,                                  /* preconfiguration */
-    ngx_http_log_init,                     /* postconfiguration */
+    ngx_stream_log_init,                   /* postconfiguration */
 
-    ngx_http_log_create_main_conf,         /* create main configuration */
+    ngx_stream_log_create_main_conf,       /* create main configuration */
     NULL,                                  /* init main configuration */
 
-    NULL,                                  /* create server configuration */
-    NULL,                                  /* merge server configuration */
-
-    ngx_http_log_create_loc_conf,          /* create location configuration */
-    ngx_http_log_merge_loc_conf            /* merge location configuration */
+    ngx_stream_log_create_srv_conf,        /* create server configuration */
+    ngx_stream_log_merge_srv_conf          /* merge server configuration */
 };
 
 
-ngx_module_t  ngx_http_log_module = {
+ngx_module_t  ngx_stream_log_module = {
     NGX_MODULE_V1,
-    &ngx_http_log_module_ctx,              /* module context */
-    ngx_http_log_commands,                 /* module directives */
-    NGX_HTTP_MODULE,                       /* module type */
+    &ngx_stream_log_module_ctx,            /* module context */
+    ngx_stream_log_commands,               /* module directives */
+    NGX_STREAM_MODULE,                     /* module type */
     NULL,                                  /* init master */
     NULL,                                  /* init module */
     NULL,                                  /* init process */
@@ -208,62 +184,33 @@ ngx_module_t  ngx_http_log_module = {
 };
 
 
-static ngx_str_t  ngx_http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
-
-
-static ngx_str_t  ngx_http_combined_fmt =
-    ngx_string("$remote_addr - $remote_user [$time_local] "
-               "\"$request\" $status $body_bytes_sent "
-               "\"$http_referer\" \"$http_user_agent\"");
-
+static ngx_int_t
+ngx_stream_log_handler(ngx_stream_session_t *s)
+{
+    u_char                     *line, *p;
+    size_t                      len, size;
+    ssize_t                     n;
+    ngx_str_t                   val;
+    ngx_uint_t                  i, l;
+    ngx_stream_log_t           *log;
+    ngx_stream_log_op_t        *op;
+    ngx_stream_log_buf_t       *buffer;
+    ngx_stream_log_srv_conf_t  *lscf;
 
-static ngx_http_log_var_t  ngx_http_log_vars[] = {
-    { ngx_string("pipe"), 1, ngx_http_log_pipe },
-    { ngx_string("time_local"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
-                          ngx_http_log_time },
-    { ngx_string("time_iso8601"), sizeof("1970-09-28T12:00:00+06:00") - 1,
-                          ngx_http_log_iso8601 },
-    { ngx_string("msec"), NGX_TIME_T_LEN + 4, ngx_http_log_msec },
-    { ngx_string("request_time"), NGX_TIME_T_LEN + 4,
-                          ngx_http_log_request_time },
-    { ngx_string("status"), NGX_INT_T_LEN, ngx_http_log_status },
-    { ngx_string("bytes_sent"), NGX_OFF_T_LEN, ngx_http_log_bytes_sent },
-    { ngx_string("body_bytes_sent"), NGX_OFF_T_LEN,
-                          ngx_http_log_body_bytes_sent },
-    { ngx_string("request_length"), NGX_SIZE_T_LEN,
-                          ngx_http_log_request_length },
-
-    { ngx_null_string, 0, NULL }
-};
-
+    ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
+                   "stream log handler");
 
-static ngx_int_t
-ngx_http_log_handler(ngx_http_request_t *r)
-{
-    u_char                   *line, *p;
-    size_t                    len, size;
-    ssize_t                   n;
-    ngx_str_t                 val;
-    ngx_uint_t                i, l;
-    ngx_http_log_t           *log;
-    ngx_http_log_op_t        *op;
-    ngx_http_log_buf_t       *buffer;
-    ngx_http_log_loc_conf_t  *lcf;
+    lscf = ngx_stream_get_module_srv_conf(s, ngx_stream_log_module);
 
-    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                   "http log handler");
-
-    lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
-
-    if (lcf->off) {
+    if (lscf->off || lscf->logs == NULL) {
         return NGX_OK;
     }
 
-    log = lcf->logs->elts;
-    for (l = 0; l < lcf->logs->nelts; l++) {
+    log = lscf->logs->elts;
+    for (l = 0; l < lscf->logs->nelts; l++) {
 
         if (log[l].filter) {
-            if (ngx_http_complex_value(r, log[l].filter, &val) != NGX_OK) {
+            if (ngx_stream_complex_value(s, log[l].filter, &val) != NGX_OK) {
                 return NGX_ERROR;
             }
 
@@ -283,13 +230,14 @@ ngx_http_log_handler(ngx_http_request_t 
             continue;
         }
 
-        ngx_http_script_flush_no_cacheable_variables(r, log[l].format->flushes);
+        ngx_stream_script_flush_no_cacheable_variables(s,
+                                                       log[l].format->flushes);
 
         len = 0;
         op = log[l].format->ops->elts;
         for (i = 0; i < log[l].format->ops->nelts; i++) {
             if (op[i].len == 0) {
-                len += op[i].getlen(r, op[i].data);
+                len += op[i].getlen(s, op[i].data);
 
             } else {
                 len += op[i].len;
@@ -314,8 +262,8 @@ ngx_http_log_handler(ngx_http_request_t 
 
             if (len > (size_t) (buffer->last - buffer->pos)) {
 
-                ngx_http_log_write(r, &log[l], buffer->start,
-                                   buffer->pos - buffer->start);
+                ngx_stream_log_write(s, &log[l], buffer->start,
+                                     buffer->pos - buffer->start);
 
                 buffer->pos = buffer->start;
             }
@@ -329,7 +277,7 @@ ngx_http_log_handler(ngx_http_request_t 
                 }
 
                 for (i = 0; i < log[l].format->ops->nelts; i++) {
-                    p = op[i].run(r, p, &op[i]);
+                    p = op[i].run(s, p, &op[i]);
                 }
 
                 ngx_linefeed(p);
@@ -346,7 +294,7 @@ ngx_http_log_handler(ngx_http_request_t 
 
     alloc_line:
 
-        line = ngx_pnalloc(r->pool, len);
+        line = ngx_pnalloc(s->connection->pool, len);
         if (line == NULL) {
             return NGX_ERROR;
         }
@@ -358,7 +306,7 @@ ngx_http_log_handler(ngx_http_request_t 
         }
 
         for (i = 0; i < log[l].format->ops->nelts; i++) {
-            p = op[i].run(r, p, &op[i]);
+            p = op[i].run(s, p, &op[i]);
         }
 
         if (log[l].syslog_peer) {
@@ -368,11 +316,11 @@ ngx_http_log_handler(ngx_http_request_t 
             n = ngx_syslog_send(log[l].syslog_peer, line, size);
 
             if (n < 0) {
-                ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
+                ngx_log_error(NGX_LOG_WARN, s->connection->log, 0,
                               "send() to syslog failed");
 
             } else if ((size_t) n != size) {
-                ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
+                ngx_log_error(NGX_LOG_WARN, s->connection->log, 0,
                               "send() to syslog has written only %z of %uz",
                               n, size);
             }
@@ -382,7 +330,7 @@ ngx_http_log_handler(ngx_http_request_t 
 
         ngx_linefeed(p);
 
-        ngx_http_log_write(r, &log[l], line, p - line);
+        ngx_stream_log_write(s, &log[l], line, p - line);
     }
 
     return NGX_OK;
@@ -390,15 +338,15 @@ ngx_http_log_handler(ngx_http_request_t 
 
 
 static void
-ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log, u_char *buf,
-    size_t len)
+ngx_stream_log_write(ngx_stream_session_t *s, ngx_stream_log_t *log,
+    u_char *buf, size_t len)
 {
-    u_char              *name;
-    time_t               now;
-    ssize_t              n;
-    ngx_err_t            err;
+    u_char                *name;
+    time_t                 now;
+    ssize_t                n;
+    ngx_err_t              err;
 #if (NGX_ZLIB)
-    ngx_http_log_buf_t  *buffer;
+    ngx_stream_log_buf_t  *buffer;
 #endif
 
     if (log->script == NULL) {
@@ -408,8 +356,8 @@ ngx_http_log_write(ngx_http_request_t *r
         buffer = log->file->data;
 
         if (buffer && buffer->gzip) {
-            n = ngx_http_log_gzip(log->file->fd, buf, len, buffer->gzip,
-                                  r->connection->log);
+            n = ngx_stream_log_gzip(log->file->fd, buf, len, buffer->gzip,
+                                    s->connection->log);
         } else {
             n = ngx_write_fd(log->file->fd, buf, len);
         }
@@ -419,7 +367,7 @@ ngx_http_log_write(ngx_http_request_t *r
 
     } else {
         name = NULL;
-        n = ngx_http_log_script_write(r, log->script, &name, buf, len);
+        n = ngx_stream_log_script_write(s, log->script, &name, buf, len);
     }
 
     if (n == (ssize_t) len) {
@@ -436,7 +384,7 @@ ngx_http_log_write(ngx_http_request_t *r
         }
 
         if (now - log->error_log_time > 59) {
-            ngx_log_error(NGX_LOG_ALERT, r->connection->log, err,
+            ngx_log_error(NGX_LOG_ALERT, s->connection->log, err,
                           ngx_write_fd_n " to \"%s\" failed", name);
 
             log->error_log_time = now;
@@ -446,7 +394,7 @@ ngx_http_log_write(ngx_http_request_t *r
     }
 
     if (now - log->error_log_time > 59) {
-        ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
+        ngx_log_error(NGX_LOG_ALERT, s->connection->log, 0,
                       ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
                       name, n, len);
 
@@ -456,69 +404,16 @@ ngx_http_log_write(ngx_http_request_t *r
 
 
 static ssize_t
-ngx_http_log_script_write(ngx_http_request_t *r, ngx_http_log_script_t *script,
-    u_char **name, u_char *buf, size_t len)
+ngx_stream_log_script_write(ngx_stream_session_t *s,
+    ngx_stream_log_script_t *script, u_char **name, u_char *buf, size_t len)
 {
-    size_t                     root;
-    ssize_t                    n;
-    ngx_str_t                  log, path;
-    ngx_open_file_info_t       of;
-    ngx_http_log_loc_conf_t   *llcf;
-    ngx_http_core_loc_conf_t  *clcf;
-
-    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-
-    if (!r->root_tested) {
-
-        /* test root directory existence */
-
-        if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
-            /* simulate successful logging */
-            return len;
-        }
-
-        path.data[root] = '\0';
-
-        ngx_memzero(&of, sizeof(ngx_open_file_info_t));
-
-        of.valid = clcf->open_file_cache_valid;
-        of.min_uses = clcf->open_file_cache_min_uses;
-        of.test_dir = 1;
-        of.test_only = 1;
-        of.errors = clcf->open_file_cache_errors;
-        of.events = clcf->open_file_cache_events;
+    ssize_t                     n;
+    ngx_str_t                   log;
+    ngx_open_file_info_t        of;
+    ngx_stream_log_srv_conf_t  *lscf;
 
-        if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
-            /* simulate successful logging */
-            return len;
-        }
-
-        if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
-            != NGX_OK)
-        {
-            if (of.err == 0) {
-                /* simulate successful logging */
-                return len;
-            }
-
-            ngx_log_error(NGX_LOG_ERR, r->connection->log, of.err,
-                          "testing \"%s\" existence failed", path.data);
-
-            /* simulate successful logging */
-            return len;
-        }
-
-        if (!of.is_dir) {
-            ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ENOTDIR,
-                          "testing \"%s\" existence failed", path.data);
-
-            /* simulate successful logging */
-            return len;
-        }
-    }
-
-    if (ngx_http_script_run(r, &log, script->lengths->elts, 1,
-                            script->values->elts)
+    if (ngx_stream_script_run(s, &log, script->lengths->elts, 1,
+                              script->values->elts)
         == NULL)
     {
         /* simulate successful logging */
@@ -528,34 +423,30 @@ ngx_http_log_script_write(ngx_http_reque
     log.data[log.len - 1] = '\0';
     *name = log.data;
 
-    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                   "http log \"%s\"", log.data);
+    ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
+                   "stream log \"%s\"", log.data);
 
-    llcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
+    lscf = ngx_stream_get_module_srv_conf(s, ngx_stream_log_module);
 
     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
 
     of.log = 1;
-    of.valid = llcf->open_file_cache_valid;
-    of.min_uses = llcf->open_file_cache_min_uses;
+    of.valid = lscf->open_file_cache_valid;
+    of.min_uses = lscf->open_file_cache_min_uses;
     of.directio = NGX_OPEN_FILE_DIRECTIO_OFF;
 
-    if (ngx_http_set_disable_symlinks(r, clcf, &log, &of) != NGX_OK) {
-        /* simulate successful logging */
-        return len;
-    }
-
-    if (ngx_open_cached_file(llcf->open_file_cache, &log, &of, r->pool)
+    if (ngx_open_cached_file(lscf->open_file_cache, &log, &of,
+                             s->connection->pool)
         != NGX_OK)
     {
-        ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
+        ngx_log_error(NGX_LOG_CRIT, s->connection->log, ngx_errno,
                       "%s \"%s\" failed", of.failed, log.data);
         /* simulate successful logging */
         return len;
     }
 
-    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                   "http log #%d", of.fd);
+    ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
+                   "stream log #%d", of.fd);
 
     n = ngx_write_fd(of.fd, buf, len);
 
@@ -566,7 +457,7 @@ ngx_http_log_script_write(ngx_http_reque
 #if (NGX_ZLIB)
 
 static ssize_t
-ngx_http_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level,
+ngx_stream_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level,
     ngx_log_t *log)
 {
     int          rc, wbits, memlevel;
@@ -602,8 +493,8 @@ ngx_http_log_gzip(ngx_fd_t fd, u_char *b
 
     pool->log = log;
 
-    zstream.zalloc = ngx_http_log_gzip_alloc;
-    zstream.zfree = ngx_http_log_gzip_free;
+    zstream.zalloc = ngx_stream_log_gzip_alloc;
+    zstream.zfree = ngx_stream_log_gzip_free;
     zstream.opaque = pool;
 
     out = ngx_pnalloc(pool, size);
@@ -624,7 +515,7 @@ ngx_http_log_gzip(ngx_fd_t fd, u_char *b
         goto done;
     }
 
-    ngx_log_debug4(NGX_LOG_DEBUG_HTTP, log, 0,
+    ngx_log_debug4(NGX_LOG_DEBUG_STREAM, log, 0,
                    "deflate in: ni:%p no:%p ai:%ud ao:%ud",
                    zstream.next_in, zstream.next_out,
                    zstream.avail_in, zstream.avail_out);
@@ -637,7 +528,7 @@ ngx_http_log_gzip(ngx_fd_t fd, u_char *b
         goto done;
     }
 
-    ngx_log_debug5(NGX_LOG_DEBUG_HTTP, log, 0,
+    ngx_log_debug5(NGX_LOG_DEBUG_STREAM, log, 0,
                    "deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
                    zstream.next_in, zstream.next_out,
                    zstream.avail_in, zstream.avail_out,
@@ -673,11 +564,11 @@ done:
 
 
 static void *
-ngx_http_log_gzip_alloc(void *opaque, u_int items, u_int size)
+ngx_stream_log_gzip_alloc(void *opaque, u_int items, u_int size)
 {
     ngx_pool_t *pool = opaque;
 
-    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pool->log, 0,
+    ngx_log_debug2(NGX_LOG_DEBUG_STREAM, pool->log, 0,
                    "gzip alloc: n:%ud s:%ud", items, size);
 
     return ngx_palloc(pool, items * size);
@@ -685,12 +576,13 @@ ngx_http_log_gzip_alloc(void *opaque, u_
 
 
 static void
-ngx_http_log_gzip_free(void *opaque, void *address)
+ngx_stream_log_gzip_free(void *opaque, void *address)
 {
 #if 0
     ngx_pool_t *pool = opaque;
 
-    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pool->log, 0, "gzip free: %p", address);
+    ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pool->log, 0,
+                   "gzip free: %p", address);
 #endif
 }
 
@@ -698,11 +590,11 @@ ngx_http_log_gzip_free(void *opaque, voi
 
 
 static void
-ngx_http_log_flush(ngx_open_file_t *file, ngx_log_t *log)
+ngx_stream_log_flush(ngx_open_file_t *file, ngx_log_t *log)
 {
-    size_t               len;
-    ssize_t              n;
-    ngx_http_log_buf_t  *buffer;
+    size_t                 len;
+    ssize_t                n;
+    ngx_stream_log_buf_t  *buffer;
 
     buffer = file->data;
 
@@ -714,7 +606,8 @@ ngx_http_log_flush(ngx_open_file_t *file
 
 #if (NGX_ZLIB)
     if (buffer->gzip) {
-        n = ngx_http_log_gzip(file->fd, buffer->start, len, buffer->gzip, log);
+        n = ngx_stream_log_gzip(file->fd, buffer->start, len, buffer->gzip,
+                                log);
     } else {
         n = ngx_write_fd(file->fd, buffer->start, len);
     }
@@ -742,16 +635,16 @@ ngx_http_log_flush(ngx_open_file_t *file
 
 
 static void
-ngx_http_log_flush_handler(ngx_event_t *ev)
+ngx_stream_log_flush_handler(ngx_event_t *ev)
 {
-    ngx_open_file_t     *file;
-    ngx_http_log_buf_t  *buffer;
+    ngx_open_file_t       *file;
+    ngx_stream_log_buf_t  *buffer;
 
     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ev->log, 0,
-                   "http log buffer flush handler");
+                   "stream log buffer flush handler");
 
     if (ev->timedout) {
-        ngx_http_log_flush(ev->data, ev->log);
+        ngx_stream_log_flush(ev->data, ev->log);
         return;
     }
 
@@ -765,8 +658,8 @@ ngx_http_log_flush_handler(ngx_event_t *
 
 
 static u_char *
-ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
+ngx_stream_log_copy_short(ngx_stream_session_t *s, u_char *buf,
+    ngx_stream_log_op_t *op)
 {
     size_t     len;
     uintptr_t  data;
@@ -784,143 +677,27 @@ ngx_http_log_copy_short(ngx_http_request
 
 
 static u_char *
-ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
+ngx_stream_log_copy_long(ngx_stream_session_t *s, u_char *buf,
+    ngx_stream_log_op_t *op)
 {
     return ngx_cpymem(buf, (u_char *) op->data, op->len);
 }
 
 
-static u_char *
-ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
-{
-    if (r->pipeline) {
-        *buf = 'p';
-    } else {
-        *buf = '.';
-    }
-
-    return buf + 1;
-}
-
-
-static u_char *
-ngx_http_log_time(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
-{
-    return ngx_cpymem(buf, ngx_cached_http_log_time.data,
-                      ngx_cached_http_log_time.len);
-}
-
-static u_char *
-ngx_http_log_iso8601(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
-{
-    return ngx_cpymem(buf, ngx_cached_http_log_iso8601.data,
-                      ngx_cached_http_log_iso8601.len);
-}
-
-static u_char *
-ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
-{
-    ngx_time_t  *tp;
-
-    tp = ngx_timeofday();
-
-    return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
-}
-
-
-static u_char *
-ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    ngx_time_t      *tp;
-    ngx_msec_int_t   ms;
-
-    tp = ngx_timeofday();
-
-    ms = (ngx_msec_int_t)
-             ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
-    ms = ngx_max(ms, 0);
-
-    return ngx_sprintf(buf, "%T.%03M", (time_t) ms / 1000, ms % 1000);
-}
-
-
-static u_char *
-ngx_http_log_status(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
-{
-    ngx_uint_t  status;
-
-    if (r->err_status) {
-        status = r->err_status;
-
-    } else if (r->headers_out.status) {
-        status = r->headers_out.status;
-
-    } else if (r->http_version == NGX_HTTP_VERSION_9) {
-        status = 9;
-
-    } else {
-        status = 0;
-    }
-
-    return ngx_sprintf(buf, "%03ui", status);
-}
-
-
-static u_char *
-ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    return ngx_sprintf(buf, "%O", r->connection->sent);
-}
-
-
-/*
- * although there is a real $body_bytes_sent variable,
- * this log operation code function is more optimized for logging
- */
-
-static u_char *
-ngx_http_log_body_bytes_sent(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    off_t  length;
-
-    length = r->connection->sent - r->header_size;
-
-    if (length > 0) {
-        return ngx_sprintf(buf, "%O", length);
-    }
-
-    *buf = '0';
-
-    return buf + 1;
-}
-
-
-static u_char *
-ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    return ngx_sprintf(buf, "%O", r->request_length);
-}
-
-
 static ngx_int_t
-ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
+ngx_stream_log_variable_compile(ngx_conf_t *cf, ngx_stream_log_op_t *op,
     ngx_str_t *value)
 {
     ngx_int_t  index;
 
-    index = ngx_http_get_variable_index(cf, value);
+    index = ngx_stream_get_variable_index(cf, value);
     if (index == NGX_ERROR) {
         return NGX_ERROR;
     }
 
     op->len = 0;
-    op->getlen = ngx_http_log_variable_getlen;
-    op->run = ngx_http_log_variable;
+    op->getlen = ngx_stream_log_variable_getlen;
+    op->run = ngx_stream_log_variable;
     op->data = index;
 
     return NGX_OK;
@@ -928,18 +705,18 @@ ngx_http_log_variable_compile(ngx_conf_t
 
 
 static size_t
-ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
+ngx_stream_log_variable_getlen(ngx_stream_session_t *s, uintptr_t data)
 {
-    uintptr_t                   len;
-    ngx_http_variable_value_t  *value;
+    uintptr_t                     len;
+    ngx_stream_variable_value_t  *value;
 
-    value = ngx_http_get_indexed_variable(r, data);
+    value = ngx_stream_get_indexed_variable(s, data);
 
     if (value == NULL || value->not_found) {
         return 1;
     }
 
-    len = ngx_http_log_escape(NULL, value->data, value->len);
+    len = ngx_stream_log_escape(NULL, value->data, value->len);
 
     value->escape = len ? 1 : 0;
 
@@ -948,11 +725,12 @@ ngx_http_log_variable_getlen(ngx_http_re
 
 
 static u_char *
-ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
+ngx_stream_log_variable(ngx_stream_session_t *s, u_char *buf,
+    ngx_stream_log_op_t *op)
 {
-    ngx_http_variable_value_t  *value;
+    ngx_stream_variable_value_t  *value;
 
-    value = ngx_http_get_indexed_variable(r, op->data);
+    value = ngx_stream_get_indexed_variable(s, op->data);
 
     if (value == NULL || value->not_found) {
         *buf = '-';
@@ -963,13 +741,13 @@ ngx_http_log_variable(ngx_http_request_t
         return ngx_cpymem(buf, value->data, value->len);
 
     } else {
-        return (u_char *) ngx_http_log_escape(buf, value->data, value->len);
+        return (u_char *) ngx_stream_log_escape(buf, value->data, value->len);
     }
 }
 
 
 static uintptr_t
-ngx_http_log_escape(u_char *dst, u_char *src, size_t size)
+ngx_stream_log_escape(u_char *dst, u_char *src, size_t size)
 {
     ngx_uint_t      n;
     static u_char   hex[] = "0123456789ABCDEF";
@@ -1029,47 +807,32 @@ ngx_http_log_escape(u_char *dst, u_char 
 
 
 static void *
-ngx_http_log_create_main_conf(ngx_conf_t *cf)
+ngx_stream_log_create_main_conf(ngx_conf_t *cf)
 {
-    ngx_http_log_main_conf_t  *conf;
+    ngx_stream_log_main_conf_t  *conf;
 
-    ngx_http_log_fmt_t  *fmt;
-
-    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));
+    conf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_log_main_conf_t));
     if (conf == NULL) {
         return NULL;
     }
 
-    if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))
+    if (ngx_array_init(&conf->formats, cf->pool, 4,
+                       sizeof(ngx_stream_log_fmt_t))
         != NGX_OK)
     {
         return NULL;
     }
 
-    fmt = ngx_array_push(&conf->formats);
-    if (fmt == NULL) {
-        return NULL;
-    }
-
-    ngx_str_set(&fmt->name, "combined");
-
-    fmt->flushes = NULL;
-
-    fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
-    if (fmt->ops == NULL) {
-        return NULL;
-    }
-
     return conf;
 }
 
 
 static void *
-ngx_http_log_create_loc_conf(ngx_conf_t *cf)
+ngx_stream_log_create_srv_conf(ngx_conf_t *cf)
 {
-    ngx_http_log_loc_conf_t  *conf;
+    ngx_stream_log_srv_conf_t  *conf;
 
-    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t));
+    conf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_log_srv_conf_t));
     if (conf == NULL) {
         return NULL;
     }
@@ -1081,14 +844,10 @@ ngx_http_log_create_loc_conf(ngx_conf_t 
 
 
 static char *
-ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
+ngx_stream_log_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
 {
-    ngx_http_log_loc_conf_t *prev = parent;
-    ngx_http_log_loc_conf_t *conf = child;
-
-    ngx_http_log_t            *log;
-    ngx_http_log_fmt_t        *fmt;
-    ngx_http_log_main_conf_t  *lmcf;
+    ngx_stream_log_srv_conf_t *prev = parent;
+    ngx_stream_log_srv_conf_t *conf = child;
 
     if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
 
@@ -1108,60 +867,32 @@ ngx_http_log_merge_loc_conf(ngx_conf_t *
     conf->logs = prev->logs;
     conf->off = prev->off;
 
-    if (conf->logs || conf->off) {
-        return NGX_CONF_OK;
-    }
-
-    conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
-    if (conf->logs == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    log = ngx_array_push(conf->logs);
-    if (log == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    ngx_memzero(log, sizeof(ngx_http_log_t));
-
-    log->file = ngx_conf_open_file(cf->cycle, &ngx_http_access_log);
-    if (log->file == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
-    fmt = lmcf->formats.elts;
-
-    /* the default "combined" format */
-    log->format = &fmt[0];
-    lmcf->combined_used = 1;
-
     return NGX_CONF_OK;
 }
 
 
 static char *
-ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+ngx_stream_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    ngx_http_log_loc_conf_t *llcf = conf;
+    ngx_stream_log_srv_conf_t *lscf = conf;
 
-    ssize_t                            size;
-    ngx_int_t                          gzip;
-    ngx_uint_t                         i, n;
-    ngx_msec_t                         flush;
-    ngx_str_t                         *value, name, s;
-    ngx_http_log_t                    *log;
-    ngx_syslog_peer_t                 *peer;
-    ngx_http_log_buf_t                *buffer;
-    ngx_http_log_fmt_t                *fmt;
-    ngx_http_log_main_conf_t          *lmcf;
-    ngx_http_script_compile_t          sc;
-    ngx_http_compile_complex_value_t   ccv;
+    ssize_t                              size;
+    ngx_int_t                            gzip;
+    ngx_uint_t                           i, n;
+    ngx_msec_t                           flush;
+    ngx_str_t                           *value, name, s;
+    ngx_stream_log_t                    *log;
+    ngx_syslog_peer_t                   *peer;
+    ngx_stream_log_buf_t                *buffer;
+    ngx_stream_log_fmt_t                *fmt;
+    ngx_stream_script_compile_t          sc;
+    ngx_stream_log_main_conf_t          *lmcf;
+    ngx_stream_compile_complex_value_t   ccv;
 
     value = cf->args->elts;
 
     if (ngx_strcmp(value[1].data, "off") == 0) {
-        llcf->off = 1;
+        lscf->off = 1;
         if (cf->args->nelts == 2) {
             return NGX_CONF_OK;
         }
@@ -1171,21 +902,21 @@ ngx_http_log_set_log(ngx_conf_t *cf, ngx
         return NGX_CONF_ERROR;
     }
 
-    if (llcf->logs == NULL) {
-        llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
-        if (llcf->logs == NULL) {
+    if (lscf->logs == NULL) {
+        lscf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_stream_log_t));
+        if (lscf->logs == NULL) {
             return NGX_CONF_ERROR;
         }
     }
 
-    lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
+    lmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_log_module);
 
-    log = ngx_array_push(llcf->logs);
+    log = ngx_array_push(lscf->logs);
     if (log == NULL) {
         return NGX_CONF_ERROR;
     }
 
-    ngx_memzero(log, sizeof(ngx_http_log_t));
+    ngx_memzero(log, sizeof(ngx_stream_log_t));
 
 
     if (ngx_strncmp(value[1].data, "syslog:", 7) == 0) {
@@ -1204,7 +935,7 @@ ngx_http_log_set_log(ngx_conf_t *cf, ngx
         goto process_formats;
     }
 
-    n = ngx_http_script_variables_count(&value[1]);
+    n = ngx_stream_script_variables_count(&value[1]);
 
     if (n == 0) {
         log->file = ngx_conf_open_file(cf->cycle, &value[1]);
@@ -1217,12 +948,12 @@ ngx_http_log_set_log(ngx_conf_t *cf, ngx
             return NGX_CONF_ERROR;
         }
 
-        log->script = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_script_t));
+        log->script = ngx_pcalloc(cf->pool, sizeof(ngx_stream_log_script_t));
         if (log->script == NULL) {
             return NGX_CONF_ERROR;
         }
 
-        ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
+        ngx_memzero(&sc, sizeof(ngx_stream_script_compile_t));
 
         sc.cf = cf;
         sc.source = &value[1];
@@ -1232,7 +963,7 @@ ngx_http_log_set_log(ngx_conf_t *cf, ngx
         sc.complete_lengths = 1;
         sc.complete_values = 1;
 
-        if (ngx_http_script_compile(&sc) != NGX_OK) {
+        if (ngx_stream_script_compile(&sc) != NGX_OK) {
             return NGX_CONF_ERROR;
         }
     }
@@ -1242,13 +973,10 @@ process_formats:
     if (cf->args->nelts >= 3) {
         name = value[2];
 
-        if (ngx_strcmp(name.data, "combined") == 0) {
-            lmcf->combined_used = 1;
-        }
-
     } else {
-        ngx_str_set(&name, "combined");
-        lmcf->combined_used = 1;
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "log format is not specified");
+        return NGX_CONF_ERROR;
     }
 
     fmt = lmcf->formats.elts;
@@ -1340,17 +1068,17 @@ process_formats:
             s.len = value[i].len - 3;
             s.data = value[i].data + 3;
 
-            ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
+            ngx_memzero(&ccv, sizeof(ngx_stream_compile_complex_value_t));
 
             ccv.cf = cf;
             ccv.value = &s;
             ccv.complex_value = ngx_palloc(cf->pool,
-                                           sizeof(ngx_http_complex_value_t));
+                                           sizeof(ngx_stream_complex_value_t));
             if (ccv.complex_value == NULL) {
                 return NGX_CONF_ERROR;
             }
 
-            if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
+            if (ngx_stream_compile_complex_value(&ccv) != NGX_OK) {
                 return NGX_CONF_ERROR;
             }
 
@@ -1402,7 +1130,7 @@ process_formats:
             return NGX_CONF_OK;
         }
 
-        buffer = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_buf_t));
+        buffer = ngx_pcalloc(cf->pool, sizeof(ngx_stream_log_buf_t));
         if (buffer == NULL) {
             return NGX_CONF_ERROR;
         }
@@ -1422,7 +1150,7 @@ process_formats:
             }
 
             buffer->event->data = log->file;
-            buffer->event->handler = ngx_http_log_flush_handler;
+            buffer->event->handler = ngx_stream_log_flush_handler;
             buffer->event->log = &cf->cycle->new_log;
             buffer->event->cancelable = 1;
 
@@ -1431,7 +1159,7 @@ process_formats:
 
         buffer->gzip = gzip;
 
-        log->file->flush = ngx_http_log_flush;
+        log->file->flush = ngx_stream_log_flush;
         log->file->data = buffer;
     }
 
@@ -1440,13 +1168,13 @@ process_formats:
 
 
 static char *
-ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+ngx_stream_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    ngx_http_log_main_conf_t *lmcf = conf;
+    ngx_stream_log_main_conf_t *lmcf = conf;
 
-    ngx_str_t           *value;
-    ngx_uint_t           i;
-    ngx_http_log_fmt_t  *fmt;
+    ngx_str_t             *value;
+    ngx_uint_t             i;
+    ngx_stream_log_fmt_t  *fmt;
 
     value = cf->args->elts;
 
@@ -1474,26 +1202,26 @@ ngx_http_log_set_format(ngx_conf_t *cf, 
         return NGX_CONF_ERROR;
     }
 
-    fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
+    fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_stream_log_op_t));
     if (fmt->ops == NULL) {
         return NGX_CONF_ERROR;
     }
 
-    return ngx_http_log_compile_format(cf, fmt->flushes, fmt->ops, cf->args, 2);
+    return ngx_stream_log_compile_format(cf, fmt->flushes, fmt->ops,
+                                         cf->args, 2);
 }
 
 
 static char *
-ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes,
+ngx_stream_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes,
     ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s)
 {
-    u_char              *data, *p, ch;
-    size_t               i, len;
-    ngx_str_t           *value, var;
-    ngx_int_t           *flush;
-    ngx_uint_t           bracket;
-    ngx_http_log_op_t   *op;
-    ngx_http_log_var_t  *v;
+    u_char                *data, *p, ch;
+    size_t                 i, len;
+    ngx_str_t             *value, var;
+    ngx_int_t             *flush;
+    ngx_uint_t             bracket;
+    ngx_stream_log_op_t   *op;
 
     value = args->elts;
 
@@ -1561,21 +1289,7 @@ ngx_http_log_compile_format(ngx_conf_t *
                     goto invalid;
                 }
 
-                for (v = ngx_http_log_vars; v->name.len; v++) {
-
-                    if (v->name.len == var.len
-                        && ngx_strncmp(v->name.data, var.data, var.len) == 0)
-                    {
-                        op->len = v->len;
-                        op->getlen = NULL;
-                        op->run = v->run;
-                        op->data = 0;
-
-                        goto found;
-                    }
-                }
-
-                if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {
+                if (ngx_stream_log_variable_compile(cf, op, &var) != NGX_OK) {
                     return NGX_CONF_ERROR;
                 }
 
@@ -1589,8 +1303,6 @@ ngx_http_log_compile_format(ngx_conf_t *
                     *flush = op->data; /* variable index */
                 }
 
-            found:
-
                 continue;
             }
 
@@ -1608,7 +1320,7 @@ ngx_http_log_compile_format(ngx_conf_t *
                 op->getlen = NULL;
 
                 if (len <= sizeof(uintptr_t)) {
-                    op->run = ngx_http_log_copy_short;
+                    op->run = ngx_stream_log_copy_short;
                     op->data = 0;
 
                     while (len--) {
@@ -1617,7 +1329,7 @@ ngx_http_log_compile_format(ngx_conf_t *
                     }
 
                 } else {
-                    op->run = ngx_http_log_copy_long;
+                    op->run = ngx_stream_log_copy_long;
 
                     p = ngx_pnalloc(cf->pool, len);
                     if (p == NULL) {
@@ -1642,16 +1354,16 @@ invalid:
 
 
 static char *
-ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+ngx_stream_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    ngx_http_log_loc_conf_t *llcf = conf;
+    ngx_stream_log_srv_conf_t *lscf = conf;
 
     time_t       inactive, valid;
     ngx_str_t   *value, s;
     ngx_int_t    max, min_uses;
     ngx_uint_t   i;
 
-    if (llcf->open_file_cache != NGX_CONF_UNSET_PTR) {
+    if (lscf->open_file_cache != NGX_CONF_UNSET_PTR) {
         return "is duplicate";
     }
 
@@ -1712,7 +1424,7 @@ ngx_http_log_open_file_cache(ngx_conf_t 
 
         if (ngx_strcmp(value[i].data, "off") == 0) {
 
-            llcf->open_file_cache = NULL;
+            lscf->open_file_cache = NULL;
 
             continue;
         }
@@ -1725,7 +1437,7 @@ ngx_http_log_open_file_cache(ngx_conf_t 
         return NGX_CONF_ERROR;
     }
 
-    if (llcf->open_file_cache == NULL) {
+    if (lscf->open_file_cache == NULL) {
         return NGX_CONF_OK;
     }
 
@@ -1735,12 +1447,12 @@ ngx_http_log_open_file_cache(ngx_conf_t 
         return NGX_CONF_ERROR;
     }
 
-    llcf->open_file_cache = ngx_open_file_cache_init(cf->pool, max, inactive);
+    lscf->open_file_cache = ngx_open_file_cache_init(cf->pool, max, inactive);
 
-    if (llcf->open_file_cache) {
+    if (lscf->open_file_cache) {
 
-        llcf->open_file_cache_valid = valid;
-        llcf->open_file_cache_min_uses = min_uses;
+        lscf->open_file_cache_valid = valid;
+        lscf->open_file_cache_min_uses = min_uses;
 
         return NGX_CONF_OK;
     }
@@ -1750,45 +1462,13 @@ ngx_http_log_open_file_cache(ngx_conf_t 
 
 
 static ngx_int_t
-ngx_http_log_init(ngx_conf_t *cf)
+ngx_stream_log_init(ngx_conf_t *cf)
 {
-    ngx_str_t                  *value;
-    ngx_array_t                 a;
-    ngx_http_handler_pt        *h;
-    ngx_http_log_fmt_t         *fmt;
-    ngx_http_log_main_conf_t   *lmcf;
-    ngx_http_core_main_conf_t  *cmcf;
-
-    lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
-
-    if (lmcf->combined_used) {
-        if (ngx_array_init(&a, cf->pool, 1, sizeof(ngx_str_t)) != NGX_OK) {
-            return NGX_ERROR;
-        }
+    ngx_stream_core_main_conf_t  *cmcf;
 
-        value = ngx_array_push(&a);
-        if (value == NULL) {
-            return NGX_ERROR;
-        }
-
-        *value = ngx_http_combined_fmt;
-        fmt = lmcf->formats.elts;
+    cmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_core_module);
 
-        if (ngx_http_log_compile_format(cf, NULL, fmt->ops, &a, 0)
-            != NGX_CONF_OK)
-        {
-            return NGX_ERROR;
-        }
-    }
-
-    cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
-
-    h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
-    if (h == NULL) {
-        return NGX_ERROR;
-    }
-
-    *h = ngx_http_log_handler;
+    cmcf->access_log_handler = ngx_stream_log_handler;
 
     return NGX_OK;
 }
--- a/src/stream/ngx_stream_script.c
+++ b/src/stream/ngx_stream_script.c
@@ -388,6 +388,73 @@ invalid_variable:
 }
 
 
+u_char *
+ngx_stream_script_run(ngx_stream_session_t *s, ngx_str_t *value,
+    void *code_lengths, size_t len, void *code_values)
+{
+    ngx_uint_t                      i;
+    ngx_stream_script_code_pt       code;
+    ngx_stream_script_engine_t      e;
+    ngx_stream_core_main_conf_t    *cmcf;
+    ngx_stream_script_len_code_pt   lcode;
+
+    cmcf = ngx_stream_get_module_main_conf(s, ngx_stream_core_module);
+
+    for (i = 0; i < cmcf->variables.nelts; i++) {
+        if (s->variables[i].no_cacheable) {
+            s->variables[i].valid = 0;
+            s->variables[i].not_found = 0;
+        }
+    }
+
+    ngx_memzero(&e, sizeof(ngx_stream_script_engine_t));
+
+    e.ip = code_lengths;
+    e.session = s;
+    e.flushed = 1;
+
+    while (*(uintptr_t *) e.ip) {
+        lcode = *(ngx_stream_script_len_code_pt *) e.ip;
+        len += lcode(&e);
+    }
+
+
+    value->len = len;
+    value->data = ngx_pnalloc(s->connection->pool, len);
+    if (value->data == NULL) {
+        return NULL;
+    }
+
+    e.ip = code_values;
+    e.pos = value->data;
+
+    while (*(uintptr_t *) e.ip) {
+        code = *(ngx_stream_script_code_pt *) e.ip;
+        code((ngx_stream_script_engine_t *) &e);
+    }
+
+    return e.pos;
+}
+
+
+void
+ngx_stream_script_flush_no_cacheable_variables(ngx_stream_session_t *s,
+    ngx_array_t *indices)
+{
+    ngx_uint_t  n, *index;
+
+    if (indices) {
+        index = indices->elts;
+        for (n = 0; n < indices->nelts; n++) {
+            if (s->variables[index[n]].no_cacheable) {
+                s->variables[index[n]].valid = 0;
+                s->variables[index[n]].not_found = 0;
+            }
+        }
+    }
+}
+
+
 static ngx_int_t
 ngx_stream_script_init_arrays(ngx_stream_script_compile_t *sc)
 {
--- a/src/stream/ngx_stream_script.h
+++ b/src/stream/ngx_stream_script.h
@@ -110,6 +110,10 @@ char *ngx_stream_set_complex_value_slot(
 
 ngx_uint_t ngx_stream_script_variables_count(ngx_str_t *value);
 ngx_int_t ngx_stream_script_compile(ngx_stream_script_compile_t *sc);
+u_char *ngx_stream_script_run(ngx_stream_session_t *s, ngx_str_t *value,
+    void *code_lengths, size_t reserved, void *code_values);
+void ngx_stream_script_flush_no_cacheable_variables(ngx_stream_session_t *s,
+    ngx_array_t *indices);
 
 void *ngx_stream_script_add_code(ngx_array_t *codes, size_t size, void *code);