Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Lua non-blocking write log     - Oracle 11g contraction table space error ORA-03297: file contains used data beyondrequested RESIZE value (Database)

- Linux how to handle file names that contain spaces and special characters (Linux)

- C ++ constant definition (Programming)

- Preps - Print within the specified range of IP addresses (Linux)

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

- RedHat / CentOS ext4 partition can not be formatted large supplementary ext4 formatting (Linux)

- Linux crontab command format example (Linux)

- Yii2 Advanced Version Copy New Project Problem Solved (Programming)

- GAMIT10.5 install and update failed Solution (Linux)

- RedHat Linux 9.0 under P4VP-MX motherboard graphics resolution of problems (Linux)

- Linux System Getting Started Tutorial: how to find information on Linux-embedded module (Linux)

- Oracle ORA-01089 failure analysis (Database)

- Profile Linux users login shell and login to read (Linux)

- The Definitive Guide to Linux NAT network connection (Linux)

- Getting the Linux shell expr use (Programming)

- Into the Java keyword instanceof (Programming)

- ActiveMQ-based shared file system HA solutions (Server)

- Use ldap implement Windows Remote Desktop Ubuntu Linux (Linux)

- Compare several ways to transfer files between Linux hosts (Linux)

- Windows 7 hard disk installation notes Debian (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:      
 
- MongoDB study notes - polymerization (Database)
- How do I switch from NetworkManager to systemd-network on Linux (Linux)
- Linux environment to build next Cocos2dx-3.3.1 (Linux)
- Configuring xdebug debugging environment in Ubuntu 14.04 under the PhpStorm (Linux)
- NIC configuration parameters under Linux (Linux)
- Syncthing: Private Security Synchronization tool to synchronize files between computers / folder (Linux)
- Compression decompression command under Linux (Linux)
- Yii2 Advanced Version Copy New Project Problem Solved (Programming)
- Database Blob data type conversion String (Programming)
- gzip, bzip2, xz, tar, zip compression, archive Detailed (Linux)
- Neo4j map data processing tab (Database)
- Present Situation and Development Trend of firewall products (Linux)
- Linux operation and maintenance of the automated installation and deployment RHEL7.0 Cobbler (Linux)
- tar decompression problems gzip: stdin: not in gzip format (Linux)
- Using Python to find a particular file extension directory (Programming)
- Use OpenSSL to generate a certificate detailed process (Linux)
- Linux file content inspection - cat, tac, no, more, less, head, tail, od (Linux)
- How to interactively create a Docker container (Server)
- Install Apache streaming media services on CentOS 6.4 (Server)
- Linux C source code (Ascii HexToBinary: Converts hexadecimal string format ASCII codes) (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.