Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Lua non-blocking write log     - floating IP in OpenStack neutron (Server)

- GitLab Installation Guide -Ubuntu 14.04 LTS (Server)

- java.net.NoRouteToHostException of Hadoop problem: no route to host (Server)

- Java, extends and implements Usage (Programming)

- How to make a U disk to install Ubuntu (Linux)

- To use Java arrays implement the order form (Programming)

- Use OpenSSL to generate a certificate (Linux)

- Source MongoDB 3.2.1 installed on CentOS6.5 (Database)

- Ubuntu 14.10 Install Ubuntu Touch Music App 2.0 (Linux)

- Java implementation linear table - represents the order of representation and chain (Programming)

- ISO image to use offline upgrade Ubuntu (Linux)

- MySQL 5.6 database code changes (Database)

- How to Start a Linux application running in the background using the terminal mode (Linux)

- Install Ubuntu 14.04 and Windows 8 / 8.1 dual-boot in UEFI mode (Linux)

- Linux fast set ip bond (Linux)

- Compile Android libwebcore.so error occurs when solving (Programming)

- Linux command to view the system status (Linux)

- The ORA-01113 error is handled with BBED without archiving (Database)

- Redis master-slave replication switch (Database)

- Linux distributed message queue RocketMQ deployment and monitoring - Dual Master (Server)

 
         
  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:      
 
- CentOS 6.7 install Nagios Tutorials (Server)
- Mhddfs: multiple smaller partitions into one large virtual storage (Linux)
- Android Qemu GPS module (Programming)
- Secondary exponential smoothing prediction method implemented in Python (Programming)
- CentOS 7 Docker build private warehouse registry (Linux)
- Dialogue UNIX:! $ # @ *% (Linux)
- Git uses a basic tutorial (Linux)
- MySQL stored procedures and triggers (Database)
- CentOS7 virtual machine starts appear Permission denied (Linux)
- Java Concurrency - processes and threads (Programming)
- Gitblit adopted Ticket collaborative development model (Linux)
- Linux systems for entry-learning - Install Go language in Linux (Linux)
- Linux installation and error under codeblocks exclude [Ubuntu 10.04] (Linux)
- C ++ pointer two third memory model (Programming)
- Experience RHEL7 new features (Linux)
- A deep understanding of Java enum (Programming)
- Heartbeat (v1, v2, pacemaker) cluster components Overview (Server)
- Kali Linux 2.0 U disk installation errors Your installation cd-rom could not be mounted (Linux)
- CentOS 6.x and CentOS7 installation RPMforge (Linux)
- Vim useful plugin: vundle (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.