Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Lua non-blocking write log     - Ubuntu install image browser and manager Phototonic 1.6.17 (Linux)

- Ubuntu 14.04 LTS compiler installation R Source Code (Linux)

- ntop monitoring software configuration and installation (Linux)

- Ubuntu 14.04 can be used to create a WIFI hotspot for Android (Linux)

- To install the iNode client on UbuntuKylin 13.10 (Linux)

- OpenSSH server configuration file for each Common Definition (Server)

- Linux network security probing tools Hping2 (Linux)

- Detailed usage history command (Linux)

- Singleton (Linux)

- Shell Programming Regular Expressions (Programming)

- Android realize RippleEffect water (Programming)

- php How to prevent being injected (Linux)

- Ceph tuning --Journal and tcmalloc (Server)

- Windows and Ubuntu dual system, repair of two ways UEFI boot (Linux)

- Hadoop2.6.3 build clusters and the development of MapReduce WIN7 by Eclipse on Linux demo (Server)

- Protobuf compiled and used on the Ubuntu 14.04 (Programming)

- ImportTsv-HBase data import tool (Database)

- New features of Java 9 HTTP2 and REPL (Programming)

- Ubuntu 14.10 / Linux Mint 17.1 Win10 compiler install rdesktop 1.8.3 Remote Desktop connection (Linux)

- Default permissions Linux file and directory permissions and hide - umask, chattr, lsattr, SUID, SGID, SBIT, file (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))
    end

    - Get input of pipe
    pipe_in = pin

    - Close output of pipe
    posix.close (pout)

    return true
end

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

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")
end

- 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.
     
         
         
         
  More:      
 
- Detailed steps - GAMIT solver (Linux)
- How to choose the correct HTTP status code (Server)
- Netfilter / Iptables Comments (Linux)
- Oracle Automatic Diagnostic Repository (Automatic Diagnostic Repository, ADR) (Database)
- Linux excellent text editor (Markdown, LaTeX, MathJax) (Linux)
- To install the latest version of Shotwell 0.18 under Ubuntu (Linux)
- Tomcat installation under Linux (Server)
- The Concept and Semantics of Java Memory Model (Programming)
- Linux system firewall defense network attacks (Linux)
- Linux use additional rights (Linux)
- How to configure Ceph stored on CentOS 7.0 (Server)
- MongoDB relations, references, index query coverage (Database)
- Ubuntu mysql stop fail to solve the problem (Database)
- JSON data normalization (normalize) (Programming)
- Detailed usage history command (Linux)
- Linux configuration Samba server (Server)
- Hadoop 2.6.0 standalone configuration and pseudo-distributed configuration under Ubuntu 14.04 (Server)
- Zypper command for SUSE Linux package management (Linux)
- Du and df show disk space usage inconsistent Causes and Treatment (Linux)
- Batch kill processes using awk command (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.