Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Lua non-blocking write log     - Internet proxy workaround can not be used under Linux YUM (Linux)

- Bash mathematical extension (Programming)

- Standardized UNIX command-line tool (Linux)

- CentOS build JDK environment (Linux)

- Android Studio quick overview of Gradle (Programming)

- MySQL & NoSQL - Memcached widget (Database)

- Linux system installation and usage instructions Wetty (Linux)

- Linux Getting Started tutorial: hard disk partition and to deal with traps (Linux)

- Let Markdown code syntax highlighting and support Django1.6 (Linux)

- Fundamentals of the Java virtual machine memory management principles (Programming)

- CentOS and RHEL to install IPython 0.11 (Linux)

- What is a logical partition management LVM, how to use in Ubuntu (Linux)

- Using C / C ++ extensions Python (Programming)

- Oracle: RETURNING clause (Database)

- shell script: a key to install LAMP, LNMP script (Server)

- Difference in MySQL VARCHAR and CHAR data format (Database)

- Oracle table compression Technology Introduction (Database)

- Oracle ORA-01691 error message, a single data file size limit problem (Database)

- View processes and threads under Linux (Linux)

- JavaScript common functions summary (Programming)

  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.
- Linux kernel source code analysis (Linux)
- Linux garbled file delete method (Linux)
- Oracle Execute to Parse perform analytical Ratio Analysis (Database)
- Linux SVN installation and configuration graphic tutorials (Server)
- How to experience Unity 8 Mir on Ubuntu 16.04 (Linux)
- You must ask yourself four questions before deploying Docker (Server)
- Java singleton mode (Singleton mode) (Programming)
- How do I switch from NetworkManager to systemd-network on Linux (Linux)
- Use mysqldump backup performed MariaDB (Database)
- Using Linux / Unix Text processing (Linux)
- RabbitMQ user roles and access control (Linux)
- Linux kernel source tree to establish load module hello (Linux)
- Puppet 3.5 Source package Installation and Configuration (Server)
- Ubuntu 14.10 / 14.04 / 12.04 How to install Kodi 14.0 RC3 (Linux)
- Ubuntu 15.04 using the Eclipse 4.4, Java 8 and WTP (Linux)
- Ubuntu install Lighttpd + Mono support .net (Linux)
- Increase Linux system security --chattr (Linux)
- Windows using SSH management Ubuntu (Linux)
- Using RAID in Linux: Create a RAID 5 (Linux)
- Android gets the global process information and the memory used by the process (Programming)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.