4

Now I use

ngx.log(ngx.ALERT , "This is my message");

method to log messages in LUA. It gives a very descriptive message as shown in following. So how can I change the log format in NGINX to just output my alert message?

2017/03/19 12:32:10 [alert] 21994#0: *9 [lua] content_by_lua(proj1.conf:159):3: **This is my message**, client: 127.0.0.1, server: , request: "GET /service/user_info?access_token=323d106b-b170-4e91-9296-ef1a38b5af19 HTTP/1.1", host: "127.0.0.1:4000"

EDIT

I use this directive in nginx http block,

error_log stderr info;
Sameera Kumarasingha
  • 2,908
  • 3
  • 25
  • 41
  • https://devdocs.io/nginx/http/ngx_http_log_module#log_format – hjpotter92 Mar 19 '17 at 08:28
  • @hjpotter92: Thanks, But how can I use log_format for error_log directive? As I see in the documentation it is for access_log directive. – Sameera Kumarasingha Mar 19 '17 at 08:43
  • 1
    it not possible to change error log format. http://stackoverflow.com/questions/4246756/is-it-possible-to-specify-custom-error-log-format-in-nginx While core nginx build a prefix http://stackoverflow.com/questions/16711573/nginx-error-log-format-documentation, nginx_lua_module also adds its own part - [lua] content_by_lua(proj1.conf:159):3:. IMO without this prefix logs are useless – Alexander Altshuler Mar 20 '17 at 10:07

2 Answers2

0

you could do io.stderr:write("This is my message")

0

the constant prefix and suffix in error_log always boring me.

as a final workaround, I open file and write to it.

// in nginx http context

lua_shared_dict file_dict 1m;

// in lua

            local file = ngx.shared.file_dict:get("log_file")
            if not file then
                file = io.open("/tmp/custom.log", "a")
                ngx.shared.file_dict:set("log_file", file)
                ngx.log(ngx.WARN, "--- open file")
            end

            file:write(string.format("str %s num %d\\n", "abc", 456))
            file:flush()
yurenchen
  • 1,897
  • 19
  • 17