$fileName = 'log.txt'; $logMessage = 'Application started successfully.'; file_put_contents($fileName, $logMessage, FILE_APPEND);
use Monolog\Logger; use Monolog\Handler\StreamHandler; $logger = new Logger('my_logger'); $logger->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING)); $logger->warning('This is a warning message.');The above code uses the Monolog package library to create and push log data to a file at a specified path. The Logger class instance can have multiple handlers to push data to different places like a syslog, email, or Slack. In the above example, StreamHandler pushes the log messages to a file specified by the path. Package Library: Monolog is a popular logging package library widely used to manage logs in PHP.