Home Backend Development PHP Tutorial How to write a logging function in php

How to write a logging function in php

Jul 29, 2016 am 09:00 AM
address log user

We want to write a log function. First we need to understand the requirements. How do we generally use the log function? For example, when the program reaches a certain step, I hope to print the value of the variable (address) $user_address to the log. We hope This is what is written in the log:

`xx-xx-xx xx:xx $user_address: xxxxx, Yangpu District, Shanghai

Then each log must be line-wrapped, with date and time,

assuming the function name is log ();

We want to call him log('usera ddr ess:user_address);

Then if $user_address is an array and I want to output all of the array to the log, what should I do?

There is a function called print_r($arg,true). The second parameter means not to output directly, but the output result is used as the return value. We know that its output result is a string.

The log function can be written like this

<code><span>log</span>(){
    <span>$args</span> = func_get_args();<span>//获得传入的所有参数的数组</span><span>$numargs</span> = func_num_args(); <span>//参数的个数</span><span>if</span> (<span>$numargs</span> == <span>0</span>) {
        <span>$log</span> = <span>""</span>;
    } elseif (<span>$numargs</span> == <span>1</span>) {
        <span>$log</span> = <span>$args</span>[<span>0</span>];
    } <span>else</span> {
        <span>$format</span> = array_shift(<span>$args</span>); <span>//分割掉函数第一个元素,并且做返回值返回,'$user_address:%s'</span><span>$log</span> = vsprintf(<span>$format</span>, <span>$args</span>); <span>//把参数代入$format中,</span>
    }
    <span>$log</span> = <span>date</span>(<span>"[Y/m/d H:i:s] "</span>) . <span>$log</span> . PHP_EOL;<span>//加上时间</span><span>$file</span> = <span>'/usr/share/nginx/html/log.log'</span>;
    <span>$fp</span> = <span>fopen</span>(<span>$file</span>, <span>'a'</span>);
    <span>fwrite</span>(<span>$fp</span>, <span>$log</span>);
    <span>fclose</span>(<span>$fp</span>);
    <span>return</span> true;
    } 
</code>
Copy after login

Usage:
1. Print the general variable $a,
log('got the value of $a: %s',$a );

2 . Print an array $arr

log('%s',print_r( $arr,true));

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces how to use PHP to write a log function, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use the math.Log2 function to calculate the base 2 logarithm of a specified number Use the math.Log2 function to calculate the base 2 logarithm of a specified number Jul 24, 2023 pm 12:14 PM

Use the math.Log2 function to calculate the base 2 logarithm of a specified number

How to solve the problem of docker mounting directory permissions How to solve the problem of docker mounting directory permissions Feb 29, 2024 am 10:04 AM

How to solve the problem of docker mounting directory permissions

Use the math.Log10 function to calculate the base 10 logarithm of a specified number Use the math.Log10 function to calculate the base 10 logarithm of a specified number Jul 25, 2023 pm 06:33 PM

Use the math.Log10 function to calculate the base 10 logarithm of a specified number

Use the log.Println function in golang to print log information Use the log.Println function in golang to print log information Nov 18, 2023 pm 12:14 PM

Use the log.Println function in golang to print log information

Common log file storage locations for Linux servers! Common log file storage locations for Linux servers! Feb 19, 2024 pm 06:20 PM

Common log file storage locations for Linux servers!

Solve golang error: cannot take the address of 'x' Solve golang error: cannot take the address of 'x' Aug 22, 2023 pm 01:04 PM

Solve golang error: cannot take the address of 'x'

Linux script study notes, log function usage tips Linux script study notes, log function usage tips Feb 19, 2024 pm 05:36 PM

Linux script study notes, log function usage tips

How to use context to implement request logging in Go How to use context to implement request logging in Go Jul 21, 2023 pm 10:13 PM

How to use context to implement request logging in Go

See all articles