Skip to content

Commit

Permalink
Generate log file name randomly generated
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalkakadiya committed Jan 10, 2024
1 parent be967cd commit ea621a3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
41 changes: 40 additions & 1 deletion admin/class-nginx-helper-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ public function nginx_helper_default_settings() {
'enable_map' => 0,
'enable_log' => 0,
'log_level' => 'INFO',
'log_file_path' => '',
'log_filesize' => '5',
'enable_stamp' => 0,
'purge_homepage_on_edit' => 1,
Expand Down Expand Up @@ -373,6 +374,45 @@ public function functional_asset_url() {

}

/**
* Retrieve log file's name.
*
* @since 2.0.0
* @return string log file name of the plugin.
*/
public function get_log_file_name() {

$options = get_site_option( 'rt_wp_nginx_helper_options', array() );

if ( ! empty( $options ) && ! empty( $options['log_file_path'] ) ) {

return $options['log_file_path'];
}

// Generate random log file name.
$file_name = wp_generate_password( 10, false );

// Append extension.
$file_name = $file_name . '.log';

$options['log_file_path'] = $file_name;

update_site_option( 'rt_wp_nginx_helper_options', $options );

return $file_name;
}

/**
* Get log file's full path.
*
* @since 2.0.0
* @return string file path.
*/
public function get_log_file_full_path() {

return $this->functional_asset_path() . $this->get_log_file_name();
}

/**
* Get latest news.
*
Expand Down Expand Up @@ -732,7 +772,6 @@ public function purge_all() {
}

if ( 'purge' === $action ) {

/**
* Fire an action after the entire cache has been purged whatever caching type is used.
*
Expand Down
4 changes: 2 additions & 2 deletions admin/class-purger.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public function log( $msg, $level = 'INFO' ) {

if ( $log_levels[ $level ] >= $log_levels[ $nginx_helper_admin->options['log_level'] ] ) {

$fp = fopen( $nginx_helper_admin->functional_asset_path() . 'nginx.log', 'a+' );
$fp = fopen( $nginx_helper_admin->get_log_file_full_path(), 'a+' );
if ( $fp ) {

fwrite( $fp, "\n" . gmdate( 'Y-m-d H:i:s ' ) . ' | ' . $level . ' | ' . $msg );
Expand All @@ -566,7 +566,7 @@ public function check_and_truncate_log_file() {
return;
}

$nginx_asset_path = $nginx_helper_admin->functional_asset_path() . 'nginx.log';
$nginx_asset_path = $nginx_helper_admin->get_log_file_full_path();

if ( ! file_exists( $nginx_asset_path ) ) {
return;
Expand Down
21 changes: 12 additions & 9 deletions admin/partials/nginx-helper-general-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,18 @@
$nginx_helper_admin->update_map();
}

$nginx_settings['log_file_path'] = $nginx_helper_admin->get_log_file_name();

update_site_option( 'rt_wp_nginx_helper_options', $nginx_settings );

echo '<div class="updated"><p>' . esc_html__( 'Settings saved.', 'nginx-helper' ) . '</p></div>';

}

$nginx_helper_settings = $nginx_helper_admin->nginx_helper_settings();
$log_path = $nginx_helper_admin->functional_asset_path();
$log_url = $nginx_helper_admin->functional_asset_url();
$nginx_helper_settings = $nginx_helper_admin->nginx_helper_settings();
$log_path = $nginx_helper_admin->functional_asset_path();
$log_path_with_file_name = $nginx_helper_admin->get_log_file_full_path();
$log_url = $nginx_helper_admin->functional_asset_url();

/**
* Get setting url for single multiple with subdomain OR multiple with subdirectory site.
Expand Down Expand Up @@ -635,11 +638,11 @@
if ( ! is_dir( $log_path ) ) {
mkdir( $log_path );
}
if ( is_writable( $log_path ) && ! file_exists( $log_path . 'nginx.log' ) ) {
$log = fopen( $log_path . 'nginx.log', 'w' );
if ( is_writable( $log_path ) && ! file_exists( $log_path_with_file_name ) ) {
$log = fopen( $log_path_with_file_name, 'w' );
fclose( $log );
}
if ( ! is_writable( $log_path . 'nginx.log' ) ) {
if ( ! is_writable( $log_path_with_file_name ) ) {
?>
<span class="error fade" style="display : block">
<p>
Expand All @@ -650,7 +653,7 @@
sprintf(
// translators: %s file url.
__( 'Check you have write permission on <strong>%s</strong>', 'nginx-helper' ),
esc_url( $log_path . 'nginx.log' )
esc_url( $log_path_with_file_name )
),
array( 'strong' => array() )
);
Expand All @@ -671,7 +674,7 @@
</th>
<td>
<code>
<?php echo esc_url( $log_path . 'nginx.log' ); ?>
<?php echo esc_url( $log_path_with_file_name ); ?>
</code>
</td>
</tr>
Expand All @@ -682,7 +685,7 @@
</label>
</th>
<td>
<a target="_blank" href="<?php echo esc_url( $log_url . 'nginx.log' ); ?>">
<a target="_blank" href="<?php echo esc_url( $log_url . $nginx_helper_admin->get_log_file_name() ); ?>">
<?php esc_html_e( 'Log', 'nginx-helper' ); ?>
</a>
</td>
Expand Down

0 comments on commit ea621a3

Please sign in to comment.