Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Lua non-blocking write log     - CentOS 6.5 installation configuration DRBD (Server)

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

- Sort search algorithm Java - application examples with recursive dichotomy (Programming)

- Installation and use GAMIT / GLOBK Software (Linux)

- Why not use the ifconfig command under RedHat Linux 5 (Linux)

- C ++ Replication Control: Assignment operators and destructors (Programming)

- Can not empty the Recycle Bin to repair problems in Ubuntu 14.04 (Linux)

- Java multithreading easy to confuse the concept (Programming)

- osprofiler use OpenStack Cinder Lane (Server)

- Alternative methods of intrusion bundled executable file new thinking (Linux)

- The multiplexed signal driving IO (Programming)

- To control based on IP address routing policy under Linux (Linux)

- MySQL 5.7.10 source code for the latest version of the installation process in detail (Database)

- Linux automatically install service components and optimize the kernel parameters (Linux)

- Depth understanding of Python character set encoding (Programming)

- CentOS yum configuration under local sources (Linux)

- MySQL5.6.17 compiler installation under CentOS (Database)

- Questions about Linux compiler u-boot (Programming)

- Oracle 10046 Event (Database)

- Process safety monitoring and protection under the Linux operating system (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:      
 
- RPM package fabrication method (Linux)
- CentOS yum source as the default setting methods in DVD (Linux)
- MongoDB data replication and data slices (Database)
- Vagrant build LNMP environment (Server)
- CentOS 6.6 shortcut command execution (Linux)
- Ubuntu system grub repair method (Linux)
- SME Linux network security policy server security (Linux)
- Java inner classes (Programming)
- Linux mention the right notes (Linux)
- Ubuntu 14.04 LTS 64-bit installation and activation Sublime Text 3 can not solve the Chinese input method to solve the problem (Linux)
- Some common Linux commands Study Notes (Linux)
- Linux user opens a number of adjustment processes (Linux)
- Using Oracle for Oracle GoldenGate to achieve a one-way data synchronization (Database)
- Install MATE desktop environment adjustment tools Mate Tweak 3.3.6 (Linux)
- Ubuntu / openSUSE users to install Veusz 1.21.1 (Linux)
- Linux operating system security can not be ignored (Linux)
- Getting Started with Linux system to learn: how to install autossh (Linux)
- Ubuntu FAQ solutions (Linux)
- SQL Beginner Guide (Database)
- Mass data storage application of MongoDB database (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.