Maybe you could look into eval()
Hopefully this is what you mean by 'independent from the main javascript'. If you mean as in variables from the outer scope will not be defined in the string, eval() will not work for you. It is treated as part of the same program.
Edit: also see this answer on specifying the scope of eval()
If you want to store the console.log in a variable, try something like this:
var script = 'console.log("Hello World");'
var consoleOut = [];
script = script.replace(/console\.log/g,"consoleOut.push");
eval(script);
Edit: as suggested by comments, eval is often not the best in terms of security. A more efficient and secure option may be Function, however the docs mention that Function
still suffers from security issues:
var script = 'console.log("Hello World");'
var consoleOut = [];
scriptFunction = new Function(script.replace(/console\.log/g,"consoleOut.push"));
scriptFunction();