Pine Script Debugging & Logging

Pine Script Console Log: How to Print Variables & Debug Code

Pine Script does not feature a JavaScript-style console.log() function. Instead, TradingView provides the dedicated log namespace: log.info(), log.warning(), and log.error(). Messages printed with these functions appear inside the dedicated Pine Logs pane, allowing developers to inspect variable values, bar states, and execution timestamps.

Debug Pine Script with AI

How logging works in TradingView Pine Script

An unconditional log call can run on every historical bar and each realtime update. The pane shows the most recent 10,000 historical messages, then appends realtime logs. Filter by event or date in code when you need to inspect earlier history. Only personal scripts generate logs; published scripts do not directly generate them.

FunctionSeverity levelColor in Pine LogsIntended purpose
log.info(message)InformationalNeutral / GrayTracking variable state, entry triggers, and parameter setups
log.warning(message)WarningAmber / OrangeUnexpected inputs, zero divisions, or edge conditions
log.error(message)ErrorRedError-level diagnostic text; log.error() does not halt execution like runtime.error()

Complete Pine Script v6 script with structured console logging

The example logs input ordering once, crossover events on confirmed bars, and a summary at the last confirmed historical bar. Logs describe conditions, not orders or fills. Historical timestamps identify bar opening time; realtime log timestamps identify the event time.

Pine Script v6: Structured debugging with log.info and log.warningpinescript
//@version=6
indicator("Debug Logger Example [v6]", overlay = true)

// Input parameters
int fastLength = input.int(9, "Fast MA Length", minval = 1)
int slowLength = input.int(21, "Slow MA Length", minval = 1)

// Sanity check input parameters
if barstate.isfirst and fastLength >= slowLength
    log.warning("Fast length ({0}) is greater than or equal to slow length ({1})", fastLength, slowLength)

// Calculations
float fastMa = ta.ema(close, fastLength)
float slowMa = ta.ema(close, slowLength)
bool bullCross = ta.crossover(fastMa, slowMa)
bool bearCross = ta.crossunder(fastMa, slowMa)

// Visual plots
plot(fastMa, "Fast EMA", color = color.green, linewidth = 2)
plot(slowMa, "Slow EMA", color = color.red, linewidth = 2)

// Conditional logging on signal events
if bullCross and barstate.isconfirmed
    log.info("Bullish Cross on bar index {0} | Close: {1} | Fast MA: {2}", 
             bar_index, str.tostring(close, "#.##"), str.tostring(fastMa, "#.##"))

if bearCross and barstate.isconfirmed
    log.info("Bearish Cross on bar index {0} | Close: {1} | Slow MA: {2}", 
             bar_index, str.tostring(close, "#.##"), str.tostring(slowMa, "#.##"))

// Log summary once on the latest confirmed bar
if barstate.islastconfirmedhistory
    log.info("Historical calculation complete. Total bars processed: {0}", bar_index + 1)

Console log vs on-chart debug methods

TradingView supports multiple complementary techniques for viewing variable data during indicator and strategy development:

  • Pine Logs (log namespace): Best for sequential event tracing, timestamped order triggers, and detailed variable dumps across historical bars.
  • Data Window & plot(): Best for inspecting continuous numeric series (like RSI or moving averages) on any historical bar by hovering the cursor.
  • On-chart labels (label.new): Best for pinning text and prices directly to specific candlesticks for instant visual confirmation.
  • Diagnostic tables (table.new): Best for persistent heads-up dashboard panels displaying live calculations in a chart corner.

Best practices for clean Pine Script debugging

Avoid common logging pitfalls that clutter output or halt script execution:

  • Use barstate.isconfirmed for closing-update logs. barstate.islast is true on every realtime update of the last bar and does not suppress intrabar repetition by itself.
  • Use str.format for clean templates: Instead of chaining string additions with plus signs, use str.format("Index: {0}, Close: {1}", bar_index, close).
  • Keep the source version and inputs with each observation. Use the pane filters and select the intended script when several scripts produce logs.
  • Keep diagnostic output focused in your personal script. Published scripts cannot generate Pine Logs directly; library log calls may be used by personal scripts.
Where Pineify fits

Pine Script AI Coding Agent

Provide the source, diagnostic output, and chart context to investigate a problem. Request a focused revision and a syntax check, then reproduce the behavior in TradingView.

Also useful: Visual Pine Editor. Configure supported indicator modules visually, then inspect the generated calculations and chart behavior.

Debug Pine Script with AI

Frequently asked questions

Debugging logs and on-chart diagnostics assist in script development and code validation. Always test execution latency and signal timing before using automated strategy alerts in live accounts.

Sources and verification