Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Lua non-blocking write log     - Java inner classes (Programming)

- Ubuntu 14.04 compile RT288x_SDK / toolchain / squashfs3.2-r2 Error (Linux)

- How to use awk command in Linux (Linux)

- C language print various graphic (Programming)

- Linux loopback adapter Driven Design (Programming)

- Python in os.path Magical (Programming)

- Bash mathematical extension (Programming)

- Linux file permissions chmod chown (Linux)

- Linux process scheduling opportunity (Programming)

- OGG-03510 Problem (Database)

- git checkout generated in the use of temporary br (Linux)

- How to Install 3.16.7 CKT2 kernel in Ubuntu 14.10, Ubuntu 14.04 and its derivative versions (Linux)

- Extended VMware Ubuntu root partition size (Linux)

- Workaround CentOS error message during compilation PHP5 common (Linux)

- Ruby and Python simple comparison (Programming)

- VirtualBox CentOS is configured as a local disk mirroring software source (Linux)

- lolcat: an output terminal rainbow effects in the Linux command-line tool (Linux)

- Install Apache streaming media services on CentOS 6.4 (Server)

- CentOS 7 How to install MySQL Server (Database)

- Use Linux built-in firewall to improve network access control (Linux)

  Lua non-blocking write log
  Add Date : 2018-11-21      
  With nginx-lua take service needs to write the log, the general nginx comes accesslog plus logrotate script to achieve, but can not meet the need to write more than one request log demand

Before using C and GO to achieve a similar code, the idea is to write the main process piping, pipes and child reading by the apache-rotatelogs write log

Lua is also used here to achieve a bit -

- Rlog is an non-blocking logging based on pipe.
- Rotatelogs of Apache is recommanded to be the output of pipe.
- Before using Rlog, you should install luaposix.

local posix = require ( "posix")
local os = require ( "os")

- Input of pipe
local pipe_in

- Init rlog
- @param Pipe_cmd pipe operator and the reveiver command.
- E.g. "| rotatelogs -l% Y% m% d% H.logfile 3600"
- @return True when success, false when fail
local function init (pipe_cmd)
    - If already inited, do nothing
    if pipe_in then return true end

    - If pipe_cmd is not start with pipe operator, return false
    if string.sub (pipe_cmd, 1, 1) ~ = "|" then return false end

    - Create pipe
    local pout, pin = posix.pipe ()

    - Fork process to execute cmd
    if posix.fork () == 0 then
        - Close stdin
        io.close (io.stdin)

        - Set output of pipe as stdin
        posix.dup2 (pout, posix.fileno (io.stdin))

        - Close input and output of pipe
        posix.close (pout)
        posix.close (pin)

        - Execute cmd
        os.execute (string.sub (pipe_cmd, 2, -1))

    - Get input of pipe
    pipe_in = pin

    - Close output of pipe
    posix.close (pout)

    return true

- Write log
local function log (log_str)
    - Write log into pipe
    posix.write (pipe_in, log_str .. " n")

return {
    init = init,
    log = log

Below is its testing program

local rlog = require ( "rlog")

- Init
ret = rlog.init ( "| rotatelogs -l% Y% m% d% H.log 3600")
if not ret then
    print ( "init fail")

- Log
rlog.log ( "Hello Rlog")

Since posix.write write pipeline is atomic, so no need to worry about multi-process or multi-threaded write confusing issue

However, this code depends luaposix library needs to be installed before using it.
- CentOS 6.5 three ways to configure the IP address (Linux)
- The difference between free command displays the buffers and cache (Linux)
- CentOS7 installation GAMIT and GMT (Linux)
- About Linux iptables firewall interview questions and answers (Linux)
- Getting Started with Linux: Nginx Web Server How to Block Specific User Agents (UA) (Server)
- JavaScript prototype and prototype chain and project combat (Programming)
- Use IP address spoofing Intrusion Prevention Firewall (Linux)
- Linux Man Page Installation (Linux)
- PPA on Ubuntu Linux installation Plank 0.8.0 (Linux)
- Unix / Linux commonly used to clean up disk space command (Linux)
- Kitematic how seamless and DockerFILE (Server)
- Linux system Perl Lite netstat (Linux)
- Java framework for parallel study - ForkJoin (Programming)
- FileZilla FTP && TLS connection settings of (Linux)
- RabbitMQ tutorial examples: the Hello RabbitMQ World Java realization (Linux)
- Set up MySQL master and slave servers under Ubuntu 14.04 (Server)
- VMware difference in three network connection (Linux)
- Red Hat Linux security settings document (Linux)
- Shell scripts quickly deploy Tomcat project (Server)
- Linux Oracle environment variable is invalid Problem Solving (Database)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.