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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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. In mathematics, the logarithm is an important concept that describes the exponential relationship of one number to another number (the so-called base). Among them, the base 2 logarithm is particularly common and is frequently used in the fields of computer science and information technology. In the Python programming language, we can calculate the base 2 logarithm of a number using the log2 function from the math library. Here is a simple code example: importmathdef

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

In Docker, the permission problem of the mounting directory can usually be solved by the following method: adding permission-related options when using the -v parameter to specify the mounting directory. You can specify the permissions of the mounted directory by adding: ro or :rw after the mounted directory, indicating read-only and read-write permissions respectively. For example: dockerrun-v/host/path:/container/path:roimage_name Define the USER directive in the Dockerfile to specify the user running in the container to ensure that operations inside the container comply with permission requirements. For example: FROMimage_name#CreateanewuserRUNuseradd-ms/bin/

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. Logarithms are a common concept in mathematics and computer science. We often use logarithms to describe the size or proportion of numbers. In computer programming, the commonly used logarithmic function is the logarithmic function with base 10. In the Python language, you can use the log10 function in the math library to calculate the base 10 logarithm of a specified number. Below we will demonstrate the use of this function through a simple code example. First, we need

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

Title: Use the log.Println function in Golang to print log information. Logs are very important in software development. They can help us track various information during program running, such as errors, warnings, debugging information, etc. In Golang, there is a built-in log package, which provides many functions to handle log output. One of the commonly used functions is log.Println, which prints log information to standard output. Below is a simple example code showing how to use log.Print

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

In Linux servers, log files are important system information files, which record many important system events, including user login information, system startup information, system security information, email-related information, various service-related information, etc. It plays a very important role. So where are server logs stored in Linux? The following is a detailed introduction. On Linux servers, common log files are generally stored in the following locations: 1. /var/log/: This directory contains most of the log files for the system and services. Some common log files include: /var/log/messages: Overall log messages of the system, including kernel, services and others

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: cannottaketheaddressof'x' During the development process of Go language, we sometimes encounter such error message: cannottaketheaddressof'x'. This error usually occurs when we try to get the address of a variable. In this article, I will explain why this error occurs and provide some solutions. The root of the problem lies in pointer operations in the Go language. A pointer is a storage variable

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 Logging is an important component when developing web applications. It helps developers track application behavior, troubleshoot issues, and monitor system health. In the Go language, we can use the context package in the standard library to implement the request logging function. The context package provides a way to pass request-scoped data to functions and methods. In a web application, a context is created for every request

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

This article describes a log script function used to record shell script execution logs. When developing shell scripts, you often need to monitor system data. For easy viewing, we can record the script running status as a log file to avoid the need to monitor on the command console all the time. Requirements: 1. The running status of the script can be recorded; 2. The time can be recorded; 3. The previous log can be deleted after the input log reaches a certain number of lines to prevent the infinite accumulation of log files; the simple requirement of requirement analysis can be passed Write a function to implement this. This function can output the log information in the script to a log file with the same name, so that developers can check and analyze the running status of the script in the future. Can be chosen based on personal preference and technical ability

See all articles