Repository: easybuilders/easybuild-framework

Issue:
`--tmp-logdir` not working
When setting `--tmp-logdir`, I was expecting the build logs etc. to be placed into the directory. However, nothing of the sort happens: The logs are still put into `--tmpdir`. A simple example:

```bash
mkdir $HOME/tmplogs
eb -l --debug --tmp-logdir=$HOME/tmplogs zlib-1.2.11.eb
```

Nothing is ever in `$HOME/tmplogs` during or after the build.

Bootstrapped EasyBuild 3.9.2 on CentOS 7.6.



Discussion:
Looking into this, it seems like the value of the log file location has been set before the configuration variable has been taken into account.

However, I'm not sure that this option does what you want, the `--tmp-logdir` option will only set a specific (temporary) destination for the easybuild logs, which will be cleaned up if a build is successful (after the log has been copied to the installation directory). You're command line is explicitly telling eb to log to the terminal (the `-l` option) so even if it worked this directory would still be empty, and if a build was successful it would also be empty without the `-l` option. What behaviour do you want exactly?
Sorry, the `-l` was just for illustration purposes in this case—I'm normally running `eb` without it.

> the --tmp-logdir option will only set a specific (temporary) destination for the easybuild logs, which will be cleaned up if a build is successful (after the log has been copied to the installation directory).

That's exactly what I'm expecting, actually :)
@boegel I'm stuck on this one. I believe the solution is to change https://github.com/easybuilders/easybuild-framework/blob/develop/easybuild/tools/build_log.py#L201 to
```
fd, logfile = tempfile.mkstemp(suffix='.log', prefix='easybuild-', dir=get_build_log_path())
```
but this leads to a circular import

Resolving patch:
diff --git a/easybuild/tools/build_log.py b/easybuild/tools/build_log.py
--- a/easybuild/tools/build_log.py
+++ b/easybuild/tools/build_log.py
@@ -191,14 +191,14 @@ def exception(self, msg, *args):
 _init_easybuildlog = fancylogger.getLogger(fname=False)
 
 
-def init_logging(logfile, logtostdout=False, silent=False, colorize=fancylogger.Colorize.AUTO):
+def init_logging(logfile, logtostdout=False, silent=False, colorize=fancylogger.Colorize.AUTO, tmp_logdir=None):
     """Initialize logging."""
     if logtostdout:
         fancylogger.logToScreen(enable=True, stdout=True, colorize=colorize)
     else:
         if logfile is None:
             # mkstemp returns (fd,filename), fd is from os.open, not regular open!
-            fd, logfile = tempfile.mkstemp(suffix='.log', prefix='easybuild-')
+            fd, logfile = tempfile.mkstemp(suffix='.log', prefix='easybuild-', dir=tmp_logdir)
             os.close(fd)
 
         fancylogger.logToFile(logfile, max_bytes=0)
diff --git a/easybuild/tools/options.py b/easybuild/tools/options.py
--- a/easybuild/tools/options.py
+++ b/easybuild/tools/options.py
@@ -1293,7 +1293,7 @@ def set_up_configuration(args=None, logfile=None, testing=False, silent=False):
     # initialise logging for main
     log, logfile = init_logging(logfile, logtostdout=options.logtostdout,
                                 silent=(testing or options.terse or search_query or silent),
-                                colorize=options.color)
+                                colorize=options.color, tmp_logdir=options.tmp_logdir)
 
     # log startup info (must be done after setting up logger)
     eb_cmd_line = eb_go.generate_cmd_line() + eb_go.args


Test patch:
diff --git a/test/framework/config.py b/test/framework/config.py
--- a/test/framework/config.py
+++ b/test/framework/config.py
@@ -39,7 +39,7 @@
 import easybuild.tools.options as eboptions
 from easybuild.tools import run
 from easybuild.tools.build_log import EasyBuildError
-from easybuild.tools.config import build_option, build_path, get_log_filename, get_repositorypath
+from easybuild.tools.config import build_option, build_path, get_build_log_path, get_log_filename, get_repositorypath
 from easybuild.tools.config import install_path, log_file_format, log_path, source_paths
 from easybuild.tools.config import BuildOptions, ConfigurationVariables
 from easybuild.tools.config import DEFAULT_PATH_SUBDIRS, init_build_options
@@ -661,6 +661,14 @@ def test_log_path(self):
         self.assertTrue(regex.match(res), "Pattern '%s' matches '%s'" % (regex.pattern, res))
         self.assertEqual(log_file_format(), 'log.txt')
 
+    def test_get_build_log_path(self):
+        """Test for build_log_path()"""
+        init_config()
+        self.assertEqual(get_build_log_path(), tempfile.gettempdir())
+        build_log_path = os.path.join(self.test_prefix, 'chicken')
+        init_config(args=['--tmp-logdir=%s' % build_log_path])
+        self.assertEqual(get_build_log_path(), build_log_path)
+
 
 def suite():
     return TestLoaderFiltered().loadTestsFromTestCase(EasyBuildConfigTest, sys.argv[1:])
diff --git a/test/framework/run.py b/test/framework/run.py
--- a/test/framework/run.py
+++ b/test/framework/run.py
@@ -103,6 +103,16 @@ def test_run_cmd_log(self):
         self.assertEqual(len(regex.findall(read_file(logfile))), 1)
         write_file(logfile, '')
 
+        # Test that we can set the directory for the logfile
+        log_path = os.path.join(self.test_prefix, 'chicken')
+        os.mkdir(log_path)
+        logfile = None
+        init_logging(logfile, silent=True, tmp_logdir=log_path)
+        logfiles = os.listdir(log_path)
+        self.assertEqual(len(logfiles), 1)
+        self.assertTrue(logfiles[0].startswith("easybuild"))
+        self.assertTrue(logfiles[0].endswith("log"))
+
     def test_run_cmd_negative_exit_code(self):
         """Test run_cmd function with command that has negative exit code."""
         # define signal handler to call in case run_cmd takes too long