22
33namespace Wizcoders \Deployme \Support ;
44
5+ use Illuminate \Support \Facades \Log ;
6+
57/**
68 * Centralized deployment logging.
79 *
810 * Handles log file creation, path resolution, and writing with consistent formatting.
911 * Returns null rather than throwing on file errors — a missing or unwritable
1012 * log directory must not block a deploy.
13+ *
14+ * It must, however, *say* so. Failing silently means a deploy that logged nothing
15+ * looks exactly like a deploy that was never fired, which is the worst state to
16+ * debug from. Every failure here is therefore a Log::warning; only a blank
17+ * logs_path — a deliberate "off" — passes quietly.
1118 */
1219class DeployLogger
1320{
@@ -24,19 +31,24 @@ class DeployLogger
2431 */
2532 public static function open (string $ repositoryPath ): ?self
2633 {
27- $ path = trim ((string ) config ('deployme.logs_path ' , '' ));
34+ $ configured = trim ((string ) config ('deployme.logs_path ' , '' ));
2835
29- if ($ path === '' ) {
36+ // Blank is a deliberate "no file logging", not a failure. Say nothing.
37+ if ($ configured === '' ) {
3038 return null ;
3139 }
3240
3341 // Relative paths in .env are relative to the repository root, not the CWD.
34- if (! Git ::isAbsolute ($ path )) {
35- $ path = $ repositoryPath . DIRECTORY_SEPARATOR . $ path ;
36- }
42+ $ path = Git ::isAbsolute ($ configured )
43+ ? $ configured
44+ : $ repositoryPath . DIRECTORY_SEPARATOR . $ configured ;
3745
38- // Create directory if it doesn't exist
39- @mkdir ($ path , 0755 , true );
46+ // The trailing is_dir() covers the race where a concurrent run wins the mkdir.
47+ if (! is_dir ($ path ) && ! @mkdir ($ path , 0755 , true ) && ! is_dir ($ path )) {
48+ self ::warn ('could not create the deploy log directory ' , $ path , $ configured );
49+
50+ return null ;
51+ }
4052
4153 // Colons are legal in a POSIX filename but not on Windows, and they make
4254 // the log awkward to scp — so H-i-s, not H:i:s.
@@ -45,6 +57,8 @@ public static function open(string $repositoryPath): ?self
4557 $ handle = @fopen ($ file , 'w ' );
4658
4759 if ($ handle === false ) {
60+ self ::warn ('could not open the deploy log file ' , $ file , $ configured );
61+
4862 return null ;
4963 }
5064
@@ -54,6 +68,27 @@ public static function open(string $repositoryPath): ?self
5468 return $ logger ;
5569 }
5670
71+ /**
72+ * Report a logging failure to the Laravel log, then let the deploy continue.
73+ *
74+ * Reports the resolved path as well as the configured one: '../deploylogs/'
75+ * looks harmless in .env but lands outside the application root, which is
76+ * usually where the permission denial comes from.
77+ */
78+ private static function warn (string $ message , string $ path , string $ configured ): void
79+ {
80+ $ parent = dirname ($ path );
81+
82+ Log ::warning ("deployme: {$ message } — the deploy will still run, but unlogged " , [
83+ 'configured ' => $ configured ,
84+ 'resolved ' => $ path ,
85+ 'parent ' => $ parent ,
86+ 'parent_exists ' => is_dir ($ parent ),
87+ 'parent_writable ' => is_writable ($ parent ),
88+ 'error ' => error_get_last ()['message ' ] ?? null ,
89+ ]);
90+ }
91+
5792 /**
5893 * Write a line to the log file.
5994 *
0 commit comments